Upcoming SlideShare
×

# Computer notes - Sorting

1,463 views
1,394 views

Published on

Selection Sort, Insertion Sort, Bubble Sort Main idea: find the smallest element put it in the first position find the next smallest element put it in the second position

1 Comment
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Thanks sir you sort my lot of problem
you http://ecomputernotes.com
site is amazine all i need are avail,there thanks to provide us

Are you sure you want to  Yes  No
Views
Total views
1,463
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
101
1
Likes
3
Embeds 0
No embeds

No notes for slide
• End of lecture 43. Start of 44
• End of lecture 44.
• ### Computer notes - Sorting

1. 1. Class No.38 Data Structures http://ecomputernotes.com
2. 2. Sorting http://ecomputernotes.com
3. 3. Sorting Integers 20 8 5 10 7 5 7 8 10 20 How to sort the integers in this array? http://ecomputernotes.com
4. 4. Elementary Sorting Algorithms <ul><li>Selection Sort </li></ul><ul><li>Insertion Sort </li></ul><ul><li>Bubble Sort </li></ul>http://ecomputernotes.com
5. 5. Selection Sort <ul><li>Main idea: </li></ul><ul><ul><li>find the smallest element </li></ul></ul><ul><ul><li>put it in the first position </li></ul></ul><ul><ul><li>find the next smallest element </li></ul></ul><ul><ul><li>put it in the second position </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>And so on, until you get to the end of the list </li></ul>http://ecomputernotes.com
6. 6. Selection Sort -- Example 19 12 7 5 http://ecomputernotes.com 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 19 12 5 a: 1 2 3 0 19 7 12 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 5 7 12 19 a: a: 1 2 3 0
7. 7. Selection Sort: Code <ul><li>void selectionSort(int *arr, int N) </li></ul><ul><li>{ </li></ul><ul><li>int posmin, count, tmp; </li></ul><ul><li>for(count=0;count<N;count++) </li></ul><ul><li>{ </li></ul><ul><li>posmin = findIndexMin(arr,count,N); </li></ul><ul><li>tmp=arr[posmin]; </li></ul><ul><li>arr[posmin]=arr[count]; </li></ul><ul><li>arr[count]=tmp; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
8. 8. Selection Sort: Code <ul><li>int findIndexMin(int *arr, int start, int N) </li></ul><ul><li>{ </li></ul><ul><li>int posmin=start; </li></ul><ul><li>int index; </li></ul><ul><li>for(index=start; index < N; index++) </li></ul><ul><li>if (arr[index]<arr[posmin]) </li></ul><ul><li>posmin=index; </li></ul><ul><li>return posmin; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
9. 9. Swap Action (SelectionSorting) http://ecomputernotes.com 5 8 20 10 7 5 7 20 10 8 5 7 8 10 20 20 8 5 10 7 5 7 8 10 20
10. 10. Selection Sort Analysis <ul><li>What is the time complexity of this algorithm? </li></ul><ul><li>Worst case == Best case == Average case </li></ul><ul><li>Each iteration performs a linear search on the rest of the array </li></ul><ul><ul><ul><li>first element N + </li></ul></ul></ul><ul><ul><ul><li>second element N-1 + </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>penultimate element 2 + </li></ul></ul></ul><ul><ul><ul><li>last element 1 </li></ul></ul></ul><ul><ul><ul><li>Total N(N+1)/2 = ( N 2 +N)/2 </li></ul></ul></ul>http://ecomputernotes.com
11. 11. Insertion Sort <ul><li>Basic idea ( sorting cards ): </li></ul><ul><ul><li>Starts by considering the first two elements of the array data, if out of order, swap them </li></ul></ul><ul><ul><li>Consider the third element, insert it into the proper position among the first three elements. </li></ul></ul><ul><ul><li>Consider the forth element, insert it into the proper position among the first four elements. </li></ul></ul><ul><ul><li>… … </li></ul></ul>http://ecomputernotes.com
12. 12. Insertion Sort -- Example http://ecomputernotes.com 1 2 3 0 12 5 7 19 a : 1 2 3 0 19 5 7 12 a : 1 2 3 0 12 19 7 5 a : 1 2 3 0 7 12 19 5 a :
13. 13. Insertion Sort: Code <ul><li>void insertionSort(int *arr, int N) </li></ul><ul><li>{ </li></ul><ul><li>int pos, count, val; </li></ul><ul><li>for(count=1; count < N; count++) </li></ul><ul><li>{ </li></ul><ul><li>val = arr[count]; </li></ul><ul><li>for(pos=count-1; pos >= 0; pos--) </li></ul><ul><li>if (arr[pos] > val) </li></ul><ul><li>arr[pos+1]=arr[pos]; </li></ul><ul><li>else break; </li></ul><ul><li>arr[pos+1] = val; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
14. 14. Insertion Sort -- animation count val pos 1 12 0 1 12 -1 19 12 http://ecomputernotes.com 1 2 3 0 12 5 7 19 a : 1 2 3 0 19 5 7 19 a : 1 2 3 0 5 7 19 a : 12 1 2 3 0 19 5 7 12 a :
15. 15. Insertion Sort -- animation (cont) count val pos 2 5 1 2 5 -1 19 12 2 5 0 5 http://ecomputernotes.com 1 2 3 0 12 19 7 5 a : 1 2 3 0 19 5 7 12 a : 1 2 3 0 5 7 12 a : 19 1 2 3 0 19 7 12 a : 19 1 2 3 0 12 19 7 12 a :
16. 16. Insertion Sort -- animation (cont) count val pos 3 7 2 3 7 0 3 7 1 19 12 7 http://ecomputernotes.com 1 2 3 0 12 19 7 5 a : 1 2 3 0 12 19 7 5 a : 1 2 3 0 12 19 19 5 a : 1 2 3 0 12 12 19 5 a : 1 2 3 0 7 12 19 5 a :
17. 17. Insertion Sort Analysis <ul><li>What is the time complexity of this algorithm? </li></ul><ul><li>Worst case > Average case > Best case </li></ul><ul><li>Each iteration inserts an element at the start of the array, shifting all sorted elements along </li></ul><ul><ul><ul><li>second element 2 + </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>penultimate element N-1 + </li></ul></ul></ul><ul><ul><ul><li>last element N </li></ul></ul></ul><ul><ul><ul><li>Total (2+N)(N-1)/2 = O(N 2 ) </li></ul></ul></ul>http://ecomputernotes.com
18. 18. Bubble Sort <ul><li>Basic idea ( lighter bubbles rise to the top ): </li></ul><ul><ul><li>Exchange neighbouring items until the largest item reaches the end of the array </li></ul></ul><ul><ul><li>Repeat for the rest of the array </li></ul></ul>http://ecomputernotes.com
19. 19. Bubble Sort -- Example a: http://ecomputernotes.com 1 2 3 0 12 7 19 5 a: 1 2 3 0 12 7 19 5 a: 19 12 7 5 1 2 3 0 a: 12 19 7 5 a: 1 2 3 0 5 12 7 19 1 2 3 0 a: 1 2 3 0 12 7 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a: 1 2 3 0 7 12 19 5 a:
20. 20. Bubble Sort: Code <ul><li>void bubbleSort(int *arr, int N){ </li></ul><ul><li>int i, temp, bound = N-1; </li></ul><ul><li>int swapped = 1; </li></ul><ul><li>while (swapped > 0 ) { </li></ul><ul><li>swapped = 0; </li></ul><ul><li>for(i=0; I < bound; i++) if ( arr[i] > arr[i+1] ) { </li></ul><ul><li>temp = arr[i]; </li></ul><ul><li>arr[i] = arr[i+1]; </li></ul><ul><li>arr[i+1] = temp; </li></ul><ul><li>swapped = i; </li></ul><ul><li>} </li></ul><ul><li>bound = swapped; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
21. 21. Bubble Sort Analysis <ul><li>What is the time complexity of this algorithm? </li></ul><ul><li>Worst case > Average case > Best case </li></ul><ul><li>Each iteration compares all the adjacent elements, swapping them if necessary </li></ul><ul><ul><ul><li>first iteration N + </li></ul></ul></ul><ul><ul><ul><li>second iteration N-1 + </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>last iteration 1 </li></ul></ul></ul><ul><ul><ul><li>Total N(1+N)/2 = O(N 2 ) </li></ul></ul></ul>http://ecomputernotes.com
22. 22. Summary <ul><li>Insertion, Selection and Bubble sort: </li></ul><ul><ul><li>Worst case time complexity is proportional to N 2 . </li></ul></ul>Best sorting routines are N log(N) http://ecomputernotes.com
23. 23. NLogN Algorithms <ul><li>Divide and Conquer </li></ul><ul><li>Merge Sort </li></ul><ul><li>Quick Sort </li></ul><ul><li>Heap Sort </li></ul>http://ecomputernotes.com
24. 24. Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 http://ecomputernotes.com 10 12 8 4 2 11 7 5
25. 25. Divide and Conquer Sort the two parts: http://ecomputernotes.com 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11
26. 26. Divide and Conquer Then merge the two parts together: http://ecomputernotes.com 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12