Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that.

Similar presentations


Presentation on theme: "Arrays. 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that."— Presentation transcript:

1 Arrays

2 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that can hold a series of values – Just a new name for matrix – Just like a matrix index, an array uses an index to access a particular value

3 Array: composite type Multiple elements/items can be stored and retrieved, easily (in constant time). Can store any kind of element – primitive types or objects themselves. Ex. int[] a; String[] s; [] ;

4 Initialization [] = new [ ]; [] ={item1,item2,item3, …,itemn}; Once constructed, size remains fixed.

5 5 Initializing an array int[] justAVector = {2, 3, 5, 7, 11, 13, 17, 19, 23}; String[] myFriendsNames = {“Ram”, “Rahim”, “Kabir”, “Gita”, “Sita”}; char firstFewDigitsAsChars[] = {‘0’, ‘1’, ‘2’, ‘3’}; boolean whichOfMyFriendsAreTall[] = {false, false, true, true, false};

6 Array: size, addressing int[] a=new int[10]; Size of an array: a.length Element in array accessed by: a[0], a[1], etc. a[ ] Array index starts at: 0 ends at: n-1 (assuming size/length of the array is n).

7 7 Array layout in memory Recall that every variable requires some space to be stored in memory – Often the compiler is responsible for allocating this space – So, every variable has an address (just like you and I have addresses) – The address is often called a reference of a variable in Java – If I try to print the value at this address, I will get the value of the variable How is an array stored in memory?

8 8 Array layout in memory Array elements stored contiguously in memory – Starting address of a[] (same as the address of a[0]), add 8 to get the address of a[1] and so on) – doubles are 64 bits in size and memory is always byte addressed (one byte is 8 bits) – Array variable ‘a’ is really a pointer to the starting location or address of the array and is called a reference. – When calling a method, for an array argument the reference is passed. So modifications to the array inside the method are reflected outside the method – passing individual array elements passes the value (private copies are made in the method to hold the value so changes are not reflected outside.)

9 9 Always pass by value Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method. – Modifying a value in a method does not have any effect on the caller – But if the value is a reference then the change affects ‘what is referred to’ and so all references will see the change. References are addresses and hence a modification of the contents at the address will have “global” visibility On the other hand non-referential argument values are just copied in the “local memory” of a method; so a modification to a value within a method always remains local (changes contents only of local memory)

10 10

11 11

12 The Sorting Problem Let S be a sequence of entities which can be ordered (that is for any 2 elements, say a, b in the sequence exactly one of the following is true a b or a=b). Sort(S) : arranges the sequence in ascending or descending order.

13 Sort algorithms The sorting problem is one of the best studied problems and many algorithms exist. It is also practically important – sorting is routinely required in almost all database centred applications.

14 Selection sort: example //green is min, | is separator // after first round, blue sorted //stop since only one left

15 Selection sort algorithm //S is the array to be sorted. sepIndex=0; loop (s.length-1) times { minI=findMinIndex(S,sepIndex); exchange(S,sepIndex,minI); sepIndex++; }

16 Selection sort - findMinIndex int findMinIndex(from) { minI=from; for(i=from+1;index<S.length;i++) { if (S[i]<S[minI]) minI=i; } return minIndex; }

17 Bubble sort: example //sort ascending, | is unsorted-sorted separator //interchange neighbours from 0 till end //after first pass //after second pass //after third pass //4th pass, no interchange Stop condition: no interchange or | is at start.

18 Bubble sort algorithm lim=S.length-1; while (bubblePass(S,lim) && lim>0) { lim--; }

19 Bubble sort - bubblePass bubblePass(to) interchange=false; for(i=0; i<to; i++) { if (S[i]>S[i+1]) { exchange contents of S[i],S[i+1] interchange=true; } return interchange;

20 Quicksort qsort(S) { p=choose_pivot(S);//can be first /* S into S1,S2 with S1 containing elements p.*/ partition(p,S,S1,S2); SS=append(qsort(S1),p,qsort(S2)); return SS; }

21 Mergesort msort(s) { /* Split S into two (almost) equal parts s1,s2*/ split(s,s1,s2); msort(s1); msort(s2); /* at this point s1, s2 are sorted */ merge(s1,s2,ss); return ss; }

22 Merge merge(s1, s2) { s=initialize to s1.length+s2.length i1=0;i2=0,i=0; while(i1<s1.length && i2<s2.length){ if(s1[i1]<s2[i2])s[i]=s1[i1++]; else s[i]=s2[i2++]; i++ } // Now copy rest of s1 or s2 to s return s;}


Download ppt "Arrays. 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that."

Similar presentations


Ads by Google