helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the...

8
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 www.cs.cityu.edu.hk/~helena Bubble Sort Bubble Sort Scan the array from left to right, exchange pairs of elements that are out-of- order. Repeat the above process for upon (N- 1) times where N is the number of records in Example: (1st pass ) X[0..7] 25 57 48 37 12 92 86 33 25 57 48 37 12 92 86 33 25 48 57 37 12 92 86 33 25 48 37 57 12 92 86 33 25 48 37 12 57 92 86 33 25 48 37 12 57 92 86 33 25 48 37 12 57 86 92 33 25 48 37 12 57 86 33 92 The last slot now has the largest data II Bubble Sort - B

Transcript of helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the...

Page 1: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 1www.cs.cityu.edu.hk/~helena

Bubble SortBubble Sort

• Scan the array from left to right, exchange pairs of elements that are out-of-order.

• Repeat the above process for upon (N-1) times where N is the number of records in the array.

Example:(1st pass )X[0..7]

25 57 48 37 12 92 86 33

25 57 48 37 12 92 86 33

25 48 57 37 12 92 86 33

25 48 37 57 12 92 86 33

25 48 37 12 57 92 86 33

25 48 37 12 57 92 86 33

25 48 37 12 57 86 92 33

25 48 37 12 57 86 33 92

The last slot now has the largest data

II Bubble Sort - Basic

Page 2: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 2www.cs.cityu.edu.hk/~helena

Bubble Sort(2nd pass )X[0..6]

25 48 37 12 57 86 33 92

This slot now has the

2nd largest data

25 48 37 12 57 86 33 92

25 37 48 12 57 86 33 92

25 37 12 48 57 86 33 92

25 37 12 48 57 86 33 92

25 37 12 48 57 86 33 92

25 37 12 48 57 33 86 92

(3rd pass )X[0..5]

25 48 37 12 57 33 86 92

This slot now has the

3rd largest data

25 48 37 12 57 33 86 92

25 37 48 12 57 33 86 92

25 37 12 48 57 33 86 92

25 37 12 48 57 33 86 92

25 37 12 48 33 57 86 92

Others’ ordering may also be improved

like the above.

II Bubble Sort - Basic

Page 3: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 3www.cs.cityu.edu.hk/~helena

Bubble SortResult: Original (N=8): 25 57 48 37 12 92 86 33

After pass 1: 25 48 37 12 57 86 33 92 (x[0..7])

After pass 2: 25 37 12 48 57 33 86 92(x[0..6])

After pass 3: 25 12 37 48 33 57 86 92 (x[0..5])

After pass 4: 12 25 37 33 48 57 86 92(x[0..4])

After pass 5: 12 25 33 37 48 57 86 92(x[0..3])

After pass 6: 12 25 33 37 48 57 86 92 (x[0..2])

After pass 7: 12 25 33 37 48 57 86 92(x[0..1])

void bubblesort(int x[ ], int N){

int up_to_pos=N-1; //prepare up_to_pos for first pass

while (up_to_pos>=1) //perform N-1 passes{

for (int j=0; j+1<=up_to_pos; j++) //process each pair{ if (x[j] > x[j+1])

swap(x[j],x[j+1]);}up_to_pos--; //prepare up_to_pos for next pass

}}

up_to_position

II Bubble Sort - Basic

Page 4: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 4www.cs.cityu.edu.hk/~helena

Insertion Sort• Insertion sort

– Repeatedly insert a new element into an already sorted list

• Example:

x[0] is sorted

Insert (33) to get x[0..1] sorted

Insert (21) to get x[0..2] sorted

Insert (84) to get x[0..3] sorted

Insert (49) to get x[0..4] sorted

Insert (50) to get x[0..5] sorted

Insert (75) to get x[0..6] sorted

III Linear Insertion Sort - Idea

Page 5: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 5www.cs.cityu.edu.hk/~helena

Example: To find a suitable position for (49), - we compare 49 with 84 :

Because 84>49, we shift 84 to the right.-Then compare 49 with 67 :

Because 67>49, we shift 67 to the right.- Then compare 49 with 33 :

Here 33 < 49!!, so 49 should be next to 33.We stop searching and put 49 there.

1 for i = 1 to n-1 /*ie. for each x[i]=33, 21, 84, .. 75 *//* find a suitable position in x[0..i] for x[i] and put x[i] there

- the search is done from x[i-1] towards x[0]- the search is stopped once the suitable position for x[i] is found- during the search, shift these neighbors to the right to make a hole for x[i]

*/2 value_i = x[i]3 neighbr_pos = i-14 while neighbr_pos >= 0 and A[neighbr_pos] > value_i5 A[neighbr_pos+1] = A[neighbr_pos] //shift the neighbour

6 neighbr_pos --; //go to next neighbour

7 x[neighbr_pos +1] = value_i

III Linear Insertion Sort - Algorithm

Page 6: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 6www.cs.cityu.edu.hk/~helena

Merge SortAt the beginning, a Mr. MergeSort is called to sort:

5 2 4 7 1 3 2 6

Then 2 other Mr. MergeSorts are called to sort:

Both of them say “Still complicated! I’ll split

them and call other Mr. MergeSorts to handle.”

Then 4 other Mr. MergeSorts are called to sort:

All of them say “Still complicated! I’ll split

them and call other Mr. MergeSorts to handle.”

Then 8 other Mr. MergeSorts are called to sort:

5 2 4 7 1 3 2 6

5 2 2 64 7 1 3

5 2 4 7 1 3 2 6

“So complicated!!, I’ll split them and call other Mr. MergeSorts to handle.”

All of them say ‘This is easy. No need to do anything.’

Page 7: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 7www.cs.cityu.edu.hk/~helena

Merge SortThen the first Mr. MergeSort succeeds and returns.

Then each of the 2 Mr. MergeSorts returns the merged numbers.

Then the 4 Mr. MergeSorts returns the merged numbers.

Then the 8 Mr. MergeSorts return.

5 2 4 7 1 3 2 6

5 2 4 7 1 3 2 6

5 2 2 64 7 1 3

5 2 4 7 1 3 2 6

1 2 2 3 4 5 6 7

2 4 5 7 1 2 3 6

2 5 2 64 7 1 3

5 2 4 7 1 3 2 6All of them say ‘This is easy. No need do anything.’

Both Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers

The 4 Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers

The first Mr. MergeSort calls his secretary Mr. Merge to merge the returned numbers

Page 8: helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Helena Wong | Dept of CS | City U. of Hong Kong

Lec-08. Sorting - 8www.cs.cityu.edu.hk/~helena

Merge Sortvoid MERGE-SORT(x, Lower_bound, Upper_bound)

Sorts the elements:

.. 5 2 4 7 1 3 2 6 ..x =

low high

void merge-sort(int x[ ], int low, int high){ if (low < high) { int mid, *buffer = new int[high-low+1]; mid = (low + high) / 2;

merge-sort(x, low, mid);merge-sort(x, mid+1, high);.. merge the two sorted lists into buffer[]

.. copy buffer[] into x[low..high] delete [] buffer; }}