1 / 81

SORTING

SORTING. Sorting is the process of arranging the elements in some logical order. Sorting are classified into following categories: External sorting:

hferguson
Download Presentation

SORTING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SORTING Sorting is the process of arranging the elements in some logical order. Sorting are classified into following categories: • External sorting: deals with sorting of the data stored in data files. This method is used when the volume of data is very large and cannot be held in computer main memory. • Internal sorting: deals with sorting the data held in memory of the computer

  2. SORTING METHODS • Bubble sort • Selection sort • Insertion sort • Bucket sort • Merge sort • Quick sort • Heap sort • Tree sort • Shell sort

  3. BUBBLE SORT • It requires n-1 passes to sort an array. • In each pass every element a[i] is compared with a[i+1], for i=0 to (n-k), where k is the pass number and if they are out of order i. e. if a[i]>a[i+1], they are swapped. • This will cause the largest element move up or bubble up. • Thus after the end of the first pass the largest element in the array will be placed in the nth position and on each successive pass, the next largest element is placed at position (n-1),(n-2)….,2 respectively

  4. BUBBLE SORT Pass1. Step1. if a[0]>a[1] then swap a[0] and a[1]. Step2. if a[1]>a[2] then swap a[1] and a[2]. Stepn-1. if a[n-2]>a[n-1] then swap a[n-2] and a[n-1]. Pass2. Step1. if a[0]>a[1] then swap a[0] and a[1]. Step2. if a[1]>a[2] then swap a[1] and a[2]. Stepn-2. if a[n-3]>a[n-2] then swap a[n-3] and a[n-2].

  5. BUBBLE SORT . . Pass k. Step1. if a[0]>a[1] then swap a[0] and a[1]. Step2. if a[1]>a[2] then swap a[1] and a[2]. Step n-k. if a[n-k+1]>a[n-k] then swap a[n-k+1] and a[n-k]. Pass n-1 Step 1 if a[0]>a[1] then swap a[0] and a[1].

  6. BUBBLE SORT Example: 12 40 3 2 15 Pass 1 Given array

  7. BUBBLE SORT Pass 2

  8. BUBBLE SORT Pass 3 pass 4

  9. ALGORITHM for(int x=0; x<n; x++) { for(int y=0; y<n-1; y++) { if(array[y]>array[y+1]) { int temp = array[y+1]; array[y+1] = array[y]; array[y] = temp; } } }

  10. ANALYSIS OF BUBBLE SORT • First pass require n-1 comparison • Second pass requires n-2 comparison • Kth pass requires n-k comparisons • Last pass requires only one comparison Therefore total comparisons are: F(n)=(n-1)+(n-2)+……+(n-k)+…3+2+1 =n(n-1)/2 =O(n2)

  11. SELECTION SORT The selection sort also requires (n-1) passes to sort an array. In the first pass, find the smallest element from elements a[0], a[1], a[2],….., a[n-1] and swap with the first element, i.e. a[0]. In the second pass, find the smallest element from elements a[1], a[2], a[3].. a[n-1] and swap with a[1] and so on.

  12. SELECTION SORT Pass1. • Find the location loc of the smallest element in the entire array, i.e. a[0],[1],a[2]…a[n-1] • Interchange a[0] & a[loc]. Then a[0] is trivially sorted. Pass2. • Find the location loc of the smallest element in the entire array, i.e. a[1],a[2]…a[n-1] • Interchange a[1] & a[loc]. Then a[0], a[1] are sorted. Passk. • Find the location loc of the smallest element in the entire array, i.e. a[k],a[k+1],a[k+2]…a[n-1] • Interchange a[k] & a[loc]. Then a[0],a[1],a[2],…a[k] are sorted. Passn-1. • Find the location loc of the smaller of the element a[n-2],a[n-1] • Interchange a[n-2] & a[loc]. Then elements a[0],a[1],a[2]….a[n-1].

  13. EXAMPLE Given array: 20 35 40 100 3 10 15 Pass 1: Loc=4 Interchange elements a[0] & a[4] i.e. 20 and 3

  14. SELECTION SORT Pass 2 Loc=5 Interchange elements a[1] & a[5] i.e. 35 and 10 Pass3 Loc=6 Interchange elements a[2] & a[6] i.e. 40 and 15 Loc=4 Pass 4 Interchange elements a[3] & a[4] i.e. 100 and 20

  15. SELECTION SORT Pass 5 Loc=5 Interchange elements a[4] & a[5] i.e. 100 and 35 Pass 6 Loc=6 Interchange elements a[5] & a[6] i.e. 100 and 40

  16. ALGORITHM Smallestelement(a,n,k,loc) Here a is linear array of size n. this sub algorithm finds the location loc of smallest element among a[k-1],a[k+1],a[k+2]…a[n-1]. Temporary variable small is used to hold the current smllest element nd j is used loop control variable. Begin set small=a[k-1] set loc=k-1 for j=k to (n-1) by 1 do if(a[j]<small) then set small = a[j] set loc=j endif endfor end

  17. ALGORITHM Selectionsort(a,n) Here a is the linear array with n elements in memory. This algorithm sorts elements into ascending order. It uses a temporary variable temp to facilitate the exchange of two values and variable I is used loop control variable Begin for i=1 to (n-1) by 1 do call smllest element(a,n,I,loc) set temp=a[i-1] set a[i-1]=a[loc] set a[loc]=temp endfor end

  18. ANALYSIS OF SELECTION SORT • First pass require n-1 comparison to find the location loc of smallest element • Second pass requires n-2 comparison • Kth pass requires n-k comparisons • Last pass requires only one comparison Therefore total comparisons are: F(n)=(n-1)+(n-2)+……+(n-k)+…3+2+1 =n(n-1)/2 =O(n2)

  19. INSERTION SORT This algorithm is very popular with bridge players when they sort their cards. In this procedure, we pick up a particular value and then insert it at the appropriate place in the sorted sub list. This algorithm also requires n-1 passes

  20. INSERTION SORT Pass1: a[1] is inserted either before or after a[0] so that a[0] and a[1] are sorted. Pass2: a[2] is inserted either before a[0] or between a[0] and a[1] or after a[1] so that the elements a[0], a[1], a[2] are sorted. Pass3: a[3] is inserted either before a[0] or between a[0] and a[1] or between a[1] and a[2] or after a[2] so that the elements a[0], a[1], a[2], a[3] are sorted. Passk: a[k] is inserted in proper place in the sorted sub array a[0], a[1], a[2],…a[k-1] so that the elements a[0], a[1], a[2],…a[k-1],a[k] are sorted. Passn-1: a[n-1] is inserted in proper place in the sorted sub array a[0], a[1], a[2],…a[n-2] so that the elements a[0], a[1], a[2],…a[n-1] are sorted.

  21. EXAMPLE Given array: 35 20 40 100 3 10 15 Pass 1: Since a[1]< a[0] insert element a[1] before a[0]

  22. INSERTION SORT Pass 2 Since a[2]>a[1] no action is performed Pass3 Since a[3]>a[2] no action is performed Pass 4 Since a[4] is less than a[3], a[2], a[1] as well as a[0] therefore insert a[4] before a[0]

  23. INSERTION SORT Pass 5 Since a[5] is less than a[4], a[3], a[2] as well as a[1] therefore insert a[5] before a[1] Pass 6 Since a[6] is less than a[5], a[4], a[3] as well as a[2] therefore insert a[6] before a[2]

  24. ALGORITHM insertionsort(a,n) Here a is the linear array with n elements in memory. This algorithm sorts elements into ascending order. It uses a temporary variable temp to facilitate the exchange of two values and variable j and k are used loop control variables. Begin for k=1 to (n-1) by 1 do set temp=a[k] set a[j]=k-1 while((temp<a[j]) and j>=0) do set a[j+1]=a[j] set j=j-1 endwhile set a[j+1]=temp endfor end

  25. ANALYSIS OF INSERTION SORT • The worst case performance occurs when the elements of the input array are in descending order • First pass require 1 comparison to find the location loc of smallest element • Second pass requires 2 comparison • Kth pass requires k-1 comparisons • Last pass requires (n-1) comparison Therefore total comparisons are: F(n)=1+2+3+…..+(n-k)+….+(n-3)+(n-2)+(n-1) =n(n-1)/2 =O(n2)

  26. Bucket/Radix sort This is used by most of the people when sorting a list of names in alphabetical order. The procedure is: • First, the names are grouped according to the first letter, thus the names are arranged in 26 classes, one for each letter of alphabet. first class consists of those names that begin with letter A, the second class consists of those names that begins with letter B, and so on. • Next, the names are grouped according to the second letter. After this step, the list of name will be sorted on first two letter. • This process is continued for number of times depending on the length of the names with maximum letters.

  27. Bucket/Radix sort • Since there are 26 letter of alphabet, we make use of 26 buckets, one for each letter of the alphabet. • After grouping these names according to their specific letter, we collect them according to order of bucket. • This new list becomes input for the next pass i.e to separate them on the next letter from left. • To sort decimal number where base (radix) is 10, we need 10 buckets are numbered from 0-9. • Unlike sorting names, decimal numbers are sorted from right to left i.e. first on unit digits, then on ten digit and so on.

  28. example 321, 150, 235, 65, 573, 789, 928, 542 0 1 2 3 4 5 6 7 8 9 Input Need 10 buckets

  29. example 321, 150, 235, 65, 573, 789, 928, 542 0 1 2 3 4 5 6 7 8 9 Input Pass 1

  30. 0 1 2 3 4 5 6 7 8 9 Input Pass 2

  31. 0 1 2 3 4 5 6 7 8 9 Input Pass 3

  32. Bucket/Radix sort After pass three, when the numbers are collected, they are in following order 65, 150, 235, 321, 542, 573, 789, 928 thus the numbers are sorted

  33. Algorithm Bucketsort(a,n) Here a is linear array of integer with n elements, the variable digitcount is used to store the number of digits in the largest number in order to control the number of passes to be performed. Begin find the largest number of the array set digitcount=no. of digits of the largest no. for pass=1 to digitcount by 1 do initialize buckets for i=1 to n-1 by 1 do set digit=obtain digit no. pass of a[i] put a[i] in bucket no. digit increment bucket count for bucket no. digit endfor collect all the numbers from buckets in order endfor end

  34. void bucket(int a[],int n) { int bucket[10][20], buckcount[10]; int i, j, k, r, digitcount=0, divisor=1, largest, passno; larget=a[0]; for(i=1; i<n; i++) /* Find the larget no*/ { if(a[i]>largest) largest =a[i]; } while(largest>0) /* Find no of digits of largest no.*/ { digitcount++; larget/=10; } for(passno=0;passno<digicount;passno++) { for(k=0;k<10;k++) buckcount[k]=0; /*inintialize bucket count */ for( i=0;i<n;i++) { r=(a[i]/divisor)%10; bucket[r][buckcount[r]++]=a[i]; } i=0; /* Collect elements from bucket */ for(k=0;k<10;k++) { for(j=0;j<buckcount[k];j++) a[i++]=bucket[k][j]; } divisor*=10; } }

  35. Analysis of bucket sort • Let us suppose the number of digits in the largest element of the given array is S. • the number of passes to be performed is n. • Then , the umber of comparisons, f(n), needed to sort the given array are f(n)=<=n*s*10 ----here 10 base of decimal number • Though, s is independent of n , but if s=n, then in worst case. f(n)=O(n2) But on the other hand, if s=log10n, then f(n)=O(nlog10n) • Thus from the above discussion , we conclude that bucket sort performs well only when the number of digits in the elements are very small.

  36. Merge Sort

  37. Divide and Conquer • Divide-and-conquer method for algorithm design: • Divide: If the input size is too large to deal with in a straightforward manner, divide the problem into two or more disjoint subproblems • Conquer: Use divide and conquer recursively to solve the subproblems • Combine: Take the solutions to the subproblems and “merge” these solutions into a solution for the original problem

  38. Merge Sort Algorithm • Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S1 and S2 , each containing about half of the elements of S. (i.e. S1 contains the firstén/2ù elements and S2 contains the remaining ën/2û elements). • Conquer: Sort sequences S1 and S2 using Merge Sort. • Combine: Put back the elements into S by merging the sorted sequences S1 and S2 into one sorted sequence

  39. Merge Sort: Algorithm Merge-Sort(A, p, r) if p < r then q¬(p+r)/2 Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) Merge(A, p, q, r) Take the smallest of the two topmost elements of sequences A[p..q] and A[q+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[p..r].

  40. MergeSort (Example) - 1

  41. MergeSort (Example) - 2

  42. MergeSort (Example) - 3

  43. MergeSort (Example) - 4

  44. MergeSort (Example) - 5

  45. MergeSort (Example) - 6

  46. MergeSort (Example) - 7

  47. MergeSort (Example) - 8

  48. MergeSort (Example) - 9

  49. MergeSort (Example) - 10

  50. MergeSort (Example) - 11

More Related