1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array...

92
1 Array and Sorting 7 6 8 5 4 9 a “a” is a variable that is the array’s name. In Java, an array is a type of object. Therefore “a” in this example is an object reference

Transcript of 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array...

Page 1: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

1

Array and Sorting

7 6 854 9a

“a” is a variable that is the array’s name. In Java, an array is a type of object. Therefore “a” in this example is an object reference (pointer pointing to the array body). The array body is used to store the array slots.

Page 2: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

2

Array index We use a[index] to refer to the array’s

element at position indexth. From last page, a[0] is 7 and a[1] is 6.

In Java, index starts at 0. Every index must be an integer. An expression

that evaluates to an integer is also ok. If b=2 and c=3, a[b+c] is a valid array element

(provided that b+c does not exceed the array’s last possible index).

a[b+c] can be used just like any other variables; example: a[b+c] += 5;

Page 3: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

3

Array Size

In Java and many similar languages, an array has a field called “length”. Its value is set automatically when

an array is created.Usage example:- a.length

Page 4: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

4

Array Creation in Java 2 main steps:

Array Declaration: defining an array variable.

Array Allocation: creating an actual object that will become the array’s body. We can use an assignment operator to make the array variable points to the object.

Page 5: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

5

Array Creation(2)int x[]; //Declaration – just a variable, no real object. x=new int[5];

//Allocation – Creating an array object with 5 slots. Each slot contains a default value of its type. For this particular example, all slots have 0.

//Then x is made to point to the array object.            

We must specify the array size.

Page 6: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

6

Using initializer list for Array Creation Let’s create array x with 1, 2

and 3 inside.

int x[] = {1,2,3};

The {1,2,3} is an initializer list.

Page 7: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

7

Array of Object

If we have defined type MyObject, we can create an array for it.

MyObject a[] = new MyObject[3];

After the creation, each element will be null. a

nullnull null

Page 8: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

8

Array of Array

int x[][] = new int[3][3];

a

0 00 0 000 00

Page 9: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

9

Array of Array (using initializer list)

int x[] = {{1,2},{3,4},{5,6}};

x

1 2 5 63 4

Page 10: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

10

Matrix We can view an array of array as a

matrix. First index -> rowSecond index -> column

Last page’s array then becomes:

65

43

21

Page 11: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

11

Omiting size at array creation int y [][] = new int [2][];

The last index is omitted. The second layer array will be null. Nevertheless, an initialization must take place

before any actual use. For this example, we can initialize 2nd layer

arrays as follows: y[0] = new int[2]; y[1] = new int[3];

It n be seen that their size do not have to be equal.

Page 12: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

12

Bubble Sort(small-large) Compare the first two value. If the

second value is smaller, swap them. 5 2 4 3 1

Then, compare the next pair (its first value may have come from the first swap). Swap the two values if the second value is smaller.

2 5 4 3 1

We repeat until we reach the final pair. Then start again, and so on.

Page 13: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

13

n values

Running time of Bubble Sort The number of swap must be

enough for moving: the largest value from left most to

right most. The smallest value from right most to

left most.

5 2 4 3 1

the first n-1 swaps are needed in order to move the value from the left most slot to the right most slot. However, the value in the right most slot can only move left one slot. Therefore we need to do the swaps for n-1 rounds.

Big O = O(n2)

Page 14: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

14

1:     public static void bubblesort(int[] array){

2:     for (int pass = 1; pass<=array.length-1; pass++)

3:         for(int element=0; element<= array.length –2; element++)

4:          if(array[element] > array[element+1])

5:          swap(array, element, element +1);

6: }

}

1:     public static void swap(int[] array, int a, int b){

2:            int temp = array[a];

3:            array[a]= array[b];

4:            array[b]= temp;

5: }

Compare and swap 1 pair

Page 15: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

15

Worst case for Bubble Sort (initially from large->small value)

5 4 3 2 1

4 5 3 2 1

4 3 5 2 1

4 3 2 5 1

4 3 2 1 5

3 4 2 1 5

3 2 4 1 5

3 2 1 4 5

1st loop: n-1 comparisons and n-1 swaps.

2nd loop: n-1 comparisons, but n-2 swaps.

No need to swap

Page 16: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

16

Each loop: n-1 comparisons. We have n-1 loops in total. Therefore we have (n-1)2 comparisons.

The number of swaps is (n-1) + (n-2) + (n-3) +…+ 1 = n(n-1)/2

bubble sort (worst case) = (n-1)2 + n(n-1)/2 *

(unit time of a swap) = (n-1)2 + n(n-1)/2 * 3 = (5n2 – 7n +2) /2

Worst case for Bubble Sort (2)

Page 17: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

17

Selection Sort1. Store the index of the first array element in

variable maxindex.2. Check each array member one by one. If a

member value is greater than a[maxindex], change maxindex to store the index of that member. Continue until all members are checked.

3. Swap the last array member with a[maxindex] (no swapping needed if both are the same member) -> maximum value is put into the right most slot.

4. Repeat 1 – 3 again for the remaining n-1 array member.

Page 18: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

18

Selection Sort Example

maxindex is still 0. Now, swap a[maxindex] and a[4]

5 4 3 1 2maxindex =0

maxindex is now 1. We then swap a[maxindex] and a[3]

2 4 3 1 5Restart:maxindex =0

n-1 comparisons, 1 swap.

n-2 comparisons, 1 swap.

Page 19: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

19

Selection Sort (Code)public static void selectionSort(int[] a){

int maxindex; //index of the largest value

for(int unsorted= a.length; unsorted > 1; unsorted--){

maxindex = 0;

for(int index= 1; index < unsorted; index++){

if(array[maxindex] < array[index])

maxindex = index;

}

if(a[maxindex] != a[unsorted -1])

swap(array, maxindex, unsorted -1);

}

}

Reduce the array size.

Check the array 1 round and updating maxindex.

Swap if not the same.

Big O = O(n2)

Page 20: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

20

Selection Sort (worst case) Time that we count:

ComparisonsAssignments

Worst case is when each loop has a swap. This happens when the data is almost sorted except the smallest value which is at the right most slot. example: 23451, , , ,

Counting Comparisons: (n-1) + (n-2) + … + 1 times. Comparisons for the swapping: (n-1) times. Swaps: n-1 times.

Total = (n2 + 7n -8) /2 This is faster than bubble sort when n is 3 or greater.

Page 21: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

21

Insertion Sort Split the array into 2 sides, left and right. The left

side is consider sorted. Therefore, at the beginning, there is only one member in the left side.

Check each member on the right side one by one. If a member is found to be of smaller value than

the last member of the left side, put that member in its correct place on the left side.

Repeat the whole steps again. Each time, the left side will grow by 1. repeat until all members are moved to the left side.

Page 22: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

22

Insertion Sort Example

6 5 3 7 4

Must bring 5 to the front (or slide 6 to the right and put 5 in its place).

5 6 3 7 4

Slide 5 and 6 one slot each. Then put 3in the first slot.

The left side is considered sorted.

Page 23: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

23

Insertion Sort Example(2)

3 5 6 7 4

Do not need to move since 7 is in its correct position.

3 5 6 7 4

Slide 5,6,7 and put 4 next to 3.

Page 24: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

24

Compare with 6 and then 5 . 6 is moved 1 position to the right. And so is 5.

When no move is possible, put 3 where 5 used to be.

public static void insertionSort(int[] a){

int index;

for(int numSorted = 1; numSorted < a.length; numSorted++){

int temp = a[numSorted];

for(index = numSorted; index >0; index--){

if(temp< a[index-1]){

a[index] = a[index –1];

} else{

break;

}

}

a[index] = temp;

}

}

This will be put in the left side.

5 6 3 7 4

Big O = O(n2)

Page 25: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

25

Worst case takes place when there are maximum number of sliding. The array is initially sorted from large to small. In each round, all members on the left side must

move.

Insertion Sort (worst case)

worst case insertion sort: number of events in each round of the outermost loop. Loop number temp< a[index-1] a[index] =a[index-1] a[index]= temp 1 1 1 1 2 2 2 1 ... … … 1 n-1 (the last loop) n-1 n-1 1

Page 26: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

26

unit time of worst case insertion sort =(1+2+..+n-1)*2 + n-1

= n(n-1) + n-1

= (n+1)(n-1) = n2 –1

Insertion Sort (worst case cont)

Faster than selection sort when n is no more than 6.

Page 27: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

27

Insertion Sort (average case)If we are at the ith outermost loop: If a[i] does not have to be moved, a

comparison temp< a[index-1] will only takes place once.

If a[i] has to be moved, there can be from 1 to i comparisons. For example, if i = = 2, a[2] and a[1] have to

be compared. (This is counted as the first comparison)

After that, if a[1] is moved to a[2], the original value of a[2] will have to be compared with a[0].

Page 28: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

28

If we only consider the number of comparisons, we can see that in the ith loop, there is an average number of comparisons as follows:

When we consider all loops, the total number of comparisons will be:

Therefore, average case is close to worst case.

Insertion Sort (average case cont.)

2

1)1(...21 21

i

i

ii

i

i

)(4

2

2

1

2

1

2

1 221

1

1

1

1

1

nnn

ii n

i

n

i

n

i

Page 29: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

29

Merge Sort Split the array into two portions.

Then go sort each portion.(Each portion can be divided. Hence we have a recursion here.)

Then combine all sorted portion.

Page 30: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

30

Combining array in merge sort (step 1)

Let us combine a (1,5,8,9) and b (2,4,6,7) Let’s have counters at the first index of both arrays. Then we create

a new array for collecting our result. Let’s call this new array -> c.

1 5 8 9 7642a cb

indexA indexB indexC

Page 31: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

31

Combining array in merge sort (step 2) Compare a[indexA] กั�บ b[indexB].

Put a smaller value into c[indexC], then move the counters that point to that value.

1 5 8 9 7642 1a cb

indexA indexB indexC

Page 32: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

32

Combining array in merge sort (step 3)

Continue comparing a[indexA] and b[indexB] and keep updating c until one array is spent.1 5 8 9 7642 1 2 4 5 76

a cb

indexA indexB indexC

Then we copy the rest into c. Finish.

Page 33: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

33

Worst case of array combination

Takes place when comparisons are needed until the last elements of both arrays.

n-1 comparisons in total (n is the size of the resulting array )

Therefore, the time for array combination is O(n).

Page 34: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

34

Code for array combinationpublic static int[] merge(int[] a, int[] b){ int aIndex = 0; int bIndex = 0; int cIndex = 0; int aLength = a.length; int bLength = b.length; int cLength = aLength + bLength; int[] c = new int[cLength]; //compare a and b then move a value into c until one array is spent. while((aIndex < aLength) && (bIndex < bLength){ if(a[aIndex]<=b[bIndex]){ c[cIndex] = a[aIndex]; aIndex++; }else{ c[cIndex] = b[bIndex]; bIndex++; } cIndex++; }

Continue next page

Page 35: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

35

Code for array combination(cont.)//copy the remaining elements into c if(aIndex == aLength){ //if a is spent. while(bIndex<bLength){ c[cIndex] = b[bIndex]; bIndex++; cIndex++; } }else{ //if b is spent. while(aIndex<aLength){ c[cIndex] = a[aIndex]; aIndex++; cIndex++; } } return c;}

Page 36: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

36

Array splitting We do not have to do any real

sorting, becauseWhen we keep dividing array, we will eventually have arrays with one element. Combining arrays with one element is an automatic sort.

Hence, combining bigger arrays will also have sorted result.

Page 37: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

37

Code for array splitting1:     public static int[] mergeSort(int[] unsort, int left, int right){2:             if(left == right){//if there’s nothing left to sort, answer with an

// array of size 1.3:             int[] x = new int[1];4:            x[0] = unsort[left];5:             return x; 6:             }7:             else if(left<right){ //if it is sortable, keep splitting the array.8:             int center = (left+right)/2;9:             int[] result1 = mergeSort(unsort,left,center);10:         int[] result2 = mergeSort(unsort,center+1,right);11:         return merge(result1,result2); 12:         }13:      }

Page 38: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

38

Running time of merge sort If there is only one member, the

time is constant. We can have it equal to 1.

When there is more than one member, the time used is the total time for the left portion, right portion, and the combination of the two. -> O(n) n

nTnT

T

)2

(2)(

1)1(

Page 39: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

39

Running time of merge sort( cont.) Divide by n through out. We will get.

1)()(

2

2 n

nT

n

nT

We keep changing n. We get a set of equations in the next page.

(1)

Page 40: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

40

Running time of merge sort( cont.)

1)(

4

)4

(

1)(

2

)2

(

8

8

4

4

n

n

n

n

Tn

nT

Tn

nT

11

)1(

2

)2(

...

TT

(2)

(3)

(x)

Page 41: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

41

Running time of merge sort( cont.)

)log(log)(

log)1()(

nnnnnnT

nTn

nT

add (1) upto (x), we will get:

It can be seen that merge sort is faster than any other previous methods.

Its limitation is – it requires space for the resulting array.

Page 42: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

42

Quick Sort 1. If there is one member or less in an array,

that array is our answer. 2. Choose a value in the array. That value

will be our pivot. 3. Move all values that are less than pivot to

the left of pivot. Move all values that are greater than pivot to the right of pivot. (For values equal to pivot, we can deal with them in many ways. The best way is to distribute them evenly on both sides of pivot.) This step is called -> partition.

    4. Now, pivot is in the right place. We then do quick sort on both sides of the original array.

5. our answer is the concatanation of quicksort(left) ++ pivot ++ quicksort(right)

Page 43: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

43

quick sort concept

3 5 6 41 pivot

Split side

3 1 4 65

quick sort quick sort

Concat.

Page 44: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

44

step 1: when array is small If there is one member or less in an

array, that array is our answer. For small size array (such as

<20), insertion sort is faster because we don’t waste time dividing portion. So for small array, we use another sorting method.

Page 45: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

45

step 2: choosing pivot You should not use the array’s

first element as pivot. Because if that array is already sorted, one side of the portion will always be empty.

Page 46: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

46

bad pivot (choosing first member)

5 4 3 12pivot

4 3 2 51

We cannot reduce problem size by half any more.

No right portion

Page 47: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

47

Good pivot random number usually gives even

partition.But random number is slow to generate.

Median of the first, middle, and last array slot.The best pivot should be the median of all array elements. But we cannot do that because it takes too much time.

This median of 3 method performs well in general experiments.

Page 48: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

48

median of 3 – the code1:             private static int pivotIndex(int[] a, int l, int r){

2:             int c = (l+r)/2;

3:             if((a[l]<=a[r] && a[l]>=a[c]) ||

4:             (a[l]>=a[r] && a[l]<=a[c]))

5:             return l;

6:             if((a[c]<=a[l] && a[c]>=a[r]) ||

7:             (a[c]>=a[l] && a[c]<=a[r])

8:             return c;

9:             return r;

10:         }

Page 49: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

49

Step 3: partitioning1. Get pivot out of the way by swapping

it with the last element.2. Let i be the index of the first position

and j be the index of the before-last position (Pivot is in the last position).

3. Keep incrementing i until a[i] >= pivot value.

4. Keep decrementing j until a[j] <= pivot value.

Page 50: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

50

5. If i is on the left of j, swap a[i] and a[j]. This is an attempt to move smaller value to the left and larger value to the right. If i is not on the left of j, go to step 8.

6. Increment i by 1 and decrement j by 1. This is just avoiding the slots that we just swap their values.

7. Start with step 3 again. 8. Swap a[i] with pivot. We will get the

array with pivot in its correct position. To its left are the smaller values and to its right are the larger values.

Page 51: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

51

Partition example

7 2 5 01 4 9 6 83pivot

swap pivot with the last member.

7 2 5 01 8 9 6 43i j

Try to increment i and decrement j.7 2 5 01 8 9 6 43

i j

Cannot move both. Must swap a[i] and a[j].

Cont.

Page 52: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

52

Partition example(cont.)

3 2 5 01 8 9 6 47i j

Try to increment i and decrement j.3 2 5 01 8 9 6 47

i jswap a[i] and a[j].

3 2 0 51 8 9 6 47i,j

Cont.

Page 53: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

53

Partition example(cont.)3 2 0 51 8 9 6 47

i,jTry to increment i and decrement j.

3 2 0 51 8 9 6 47ij Now I is not

smaller than j. we swap a[i] and pivot.

3 2 0 41 8 9 6 57

Less than 4 Greater than 4

Page 54: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

54

Partititioning value that is equal to: 1st method

i stop but j does not stop: not good because the values will be on one side only.

7 4 2 04 8 9 6 44

pivot

jiTry to increment i and decrement j.

7 4 2 04 8 9 6 44i jswap a[i] and a[j].

0 4 2 74 8 9 6 44i j

Cont.

Page 55: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

55

Partititioning value that is equal to: 1st method (cont.)

0 4 2 74 8 9 6 44i j

Try to increment i and decrement j.

0 4 2 74 8 9 6 44i j

swap a[i] and a[j].0 2 4 74 8 9 6 44

ijPivot will be swapped here.

Page 56: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

56

Partititioning value that is equal to: 2nd method

i และ j MOVE PAST ALL pivot values. Still not good enough because if we have the following:

1 1 1 11 1 1 1 11i

j

i and j are at array’s end. Pivot will be at array’s end too. Not balance.

Page 57: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

57

Partititioning value that is equal to: 3rd method

i and j both stop when encountering a pivot value.

Unnecessary swap will take place. Last page, swapping at all steps.

Good points: even array portions. Faster in a long term.

Page 58: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

58

quick sort: codeprivate static void quicksort(int[] a,int l, int r){

if(l+CUTOFF<=r)

{

//find pivotใ int pIndex = pivotIndex(a,l,r);

//get pivot out of the way.

swap(a,pIndex,r);

int pivot = a[r];

Page 59: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

59

//start partitioning

int i=l, j=r-1;

for( ; ; )

{

while(i<r && a[i]<pivot)i++;

while(j>l && a[j]>pivot)j--;

if(i<j){

swap(a,i,j);

i++;

j--;

}else //if I exceeds j, we cannot swap them. We must get //out of the loop.

break;

}

Do not let index go beyond the array’s edge.

swap

Page 60: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

60

//sawp pivot into its correct position

swap(a,i,r);

//quick sort on subarrays

quicksort(a,l,i-1);

quicksort(a,i+1,r);

}

else

insertionSort(a,l,r);

}

Page 61: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

61

Quick sort can still be improved When choosing the pivot

Sort the 3 elements when doing the median of three.

When moving pivot out of the way, swap pivot with the value in the slot just before the last slot.

Try to execute it and compare with the original quick sort. Try it when:

when the data is 2,3,4,…,n-1,n,1. When the data is sorted from large to

small.

Page 62: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

62

quick sort running time For easy calculation, let’s assume we use a

random pivot and do not use insertion sort when the array is small.

Let T(n) = running time when working with an array of size n. Let T(0)=1, T(1)=1. For other T(n), the running time is the sum of:

Time for choosing pivot: -> constant. (negligible) Time for partitioning:-> depends on array size.

Let it be c*n. Time for quick sort on left and right subarray.

Page 63: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

63

quick sort running time(cont.)

cninTiTnT )1()()(

Let the left subarray has size i.

Page 64: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

64

Worst caseTakes place when pivot is always a smallest

value. In such situation, the array size is only reduced by 1 each time.

cnTnTnT )0()1()(

=1 -> negligibleCont.

Page 65: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

65

Worst case (cont.)

2*)1()2(

...

)2()3()2(

)1()2()1(

)1()(

cTT

ncnTnT

ncnTnT

cnnTnT

Add them all up. Cont.

Page 66: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

66

Worst case (cont 2.)

To sum up, worst case running time is similar to other sorting methods.

)()1()(

))1(...432()1()(

2

2

nicTnT

nncTnTn

i

Page 67: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

67

Best case Takes place when the array can always

be divided evenly. The calculation here is similar to merge

sort. The equation will become:

cnnTnTnT )2/()2/()(Cont.

Page 68: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

68

Best case (cont.)

cTT

cn

nT

n

nT

cn

nT

n

nT

cn

nT

n

nT

1

)1(

2

)2(

...8/

)8/(

4/

)4/(4/

)4/(

2/

)2/(2/

)2/()(

Add them.

Cont.

Page 69: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

69

Best case (cont 2.)

The same level as merge sort.

)log(log)(

log1

)1()(

nnncnnnT

ncT

n

nT

Page 70: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

70

Average case Each subarray size can be from 0 to n-

1.Subarray cannot have size == n

because we do not count our pivot.For every size to have equal chance

of happening, each size has a probability of = 1/n.

So our equation becomes:

cnjTn

nTn

j

1

0

)(1

*2)( Cont.

Page 71: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

71

Average case (cont.) Multiply by n, we get:

21

0

)(2)( cnjTnnTn

j

Change n to n-1, we get:

(avg1)

22

0

)1()(2)1()1(

ncjTnTnn

j

(avg2)

Cont.

Page 72: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

72

Average case (cont 2.) Do (avg1)-(avg2), we get:

ccnnTnTnnnT 2)1(2)1()1()(

We can ignore c. Then divide by n(n+1), we get:

1

2)1(

1

)(

n

c

n

nT

n

nT

Cont.

Page 73: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

73

Average case (cont 3.) We can then form a set of equations:

3

2

2

)1(

3

)2(

...1

2

2

)3(

1

)2(

2

1

)2()1(

cTT

n

c

n

nT

n

nTn

c

n

nT

n

nT

Add them all.( the last page too)

Cont.

Page 74: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

74

Average case (cont 4.) We get:

1

3

12

2

)1(

1

)( n

i ic

T

n

nT

The sum is a harmonic number, with the following formula:

421 120

1

12

1

2

1ln

1

nnnn

i

n

i

5772.0,256

10,1

6

nn

(avg3)

Cont.

Page 75: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

75

Average case (cont 5.) use harmonic number in avg3:

)2

11

120

1

12

1

2

1(ln2

2

)1(

1

)(42

nnn

ncT

n

nT

The right hand side is dominated by ln n. Therefore it is O(log n)

When we multiply n+1 to the whole thing:

)log()( nnnT

Page 76: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

76

Bucket sort We know where each value will go, so there is no

comparison. Example: putting each card in a 52-card deck on a table.

We only need to prepare some space for each card. When we look at a card, just put it at its provided

space. Therefore, picking a card means sorting it

automatically. The running time is O(n). A space for each card is called a bucket. For this

example, one bucket stores one object.

Page 77: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

77

If we have n numbers in a range of 1 to m. (n<m). We can order them by creating an array of size m. Let each array slot has 0 initially.

Read each number, for number k, we increment a[k] by 1.

At the end we will get a frequency of each number. We can then read the result array and print out the answer.

Time = O(n) for data reading and O(m) for result printing.

Bucket sort (cont.)

Page 78: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

78

a bucket may store more than one distinct objects. Example: sorting exam papers from 49 students:

At collection time, an examiner can divide students into 5 groups (1-9,10-19,…,40-49).

Within a group, we can use a sorting method such as insertion sort.

After sorting within a group, simply put all groups in sequence.

The running time depends on the method used to sort within buckets.

Bucket sort (cont 2.)

Page 79: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

79

Radix sort A kind of bucket sort. It’s actually doing the bucket sort

multiple times. Each time, we use a part of data to

determine buckets. Example: sorting number

143 002 013 165328Cont.

Page 80: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

80

Read the above array from left to right. Use the least significant digit to determine buckets.

We will get buckets 0-9 according to the least significant digit. 002 143,013 165 328

Radix sort (cont.)143 002 013 165328

Put them back002 143 013 328165

Cont.

Page 81: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

81

Continue by reading the above array from left to right again. Use the next-to-least significant digit to determine buckets. We will get the following buckets: 002 013 328 143 165

Radix sort (cont 2.)002 143 013 328165

Put them back002 013 328 165143

Cont.

Page 82: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

82

Repeat again, with the next significant digit as bucket indicator.

We get: 002,013 143,165 328

Radix sort (cont 3)

Put them back

002 013 143 328165

002 013 328 165143

Sorted!

Page 83: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

83

Code: finding digit d of a number n

public static int digitTh(int n, int d){

if (d == 0)

return n%10;

else

return digitTh(n/10,d-1);

}

Time = O(d)

Page 84: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

84

Code: dividing array into 10 buckets, with the d-th digit as a bucket indicator

public static void bucketing(int data[], int d){ int i,j,value; //10 buckets, each is a vector (growable //array) Vector bucket[] = new Vector[10]; for(j=0;j<10;j++) bucket[j] = new Vector();

Cont.

Page 85: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

85

//put things in buckets

int n =data.length;

for(i=0;i<n;i++){

value = data[i];

j = digitTh(value,d);

bucket[j].add(new Integer(value));

}

Cont.

Page 86: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

86

//put data back in original array, from // back to front. i=n; for(j=9;j>=0;j--){ while(!bucket[j].isEmpty()){ i- -; value= ((Integer)bucket[j].remove()).intValue(); data[i]=value; } }}

Page 87: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

87

Code: radix sort

public static void radixSort(int data[], int size){

for(int j=0;j<size; j++)

bucketing(data,j);

}

Page 88: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

88

Tip: object comparison in Java

public boolean equals(Object obj)

x.equals(y) returns true only when x and y point to the same object.

Many classes overwrite this method in order to allow objects to be compared by their contents. -> example: String

Page 89: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

89

object comparison in Java (cont.) Comparable Interface Any class that implements this interface must

have the following method:public int compareTo(Object o)

Compare this object and o.Return a negative value if this is smaller than

o. Return a positive value if this is larger than o. Return 0 if the two objects have the same

value.

Page 90: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

90

Comparator Interface Any class that implements this

interface must have the following method:public int compare(Object o1, Object o2)

This method compares o1 and o2. Return a negative number if o1 is smaller

than o2. Return a positive number if o1 is larger

than o2. Return 0 if o1 and o2 are equal.

object comparison in Java (cont 2.)

Page 91: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

91

object comparison in Java (cont 3.)

There is also another method needed for a class that implements Comparator.

public boolean equals(Object obj)

Compares this Comparator with another comparator (obj). return true if obj is a Comparator that impose

the same ordering as this.

Page 92: 1 Array and Sorting 768549 a “ a ” is a variable that is the array ’ s name. In Java, an array is a type of object. Therefore “ a ” in this example is.

92

FIN