1 / 55

Sorting

Lecture 8. Sorting. Prof. Sin-Min Lee Department of Computer Science. Preview for today’s presentation. What is sorting? Why do we sort? Three sorting methods Insertion Sort Selection Sort Bubble Sort Which method is better?. What is sorting?.

maya-stokes
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. Lecture 8 Sorting Prof. Sin-Min Lee Department of Computer Science

  2. Preview for today’s presentation • What is sorting? • Why do we sort? • Three sorting methods • Insertion Sort • Selection Sort • Bubble Sort • Which method is better?

  3. What is sorting? • It is a process of re-arranging a given set of objects in a specific order. (in ascending or descending order) • Ex: Objects are sorted in telephone books, in the tables of contents, in dictionaries, in income tax files, in libraries, in warehouses and almost everywhere that stored objects have to be searched and retrieved.

  4. Why do we sort? • It is a relevant and essential activity in data processing. • It is efficient to search in a ordered data set than an unordered data set.

  5. Sorting methods / algorithms

  6. Before proceeding . . . Terminology and Notation We are given items a1, a2, a3, . . . . . . , an then, we want to sort these items into an order ak1, ak2,ak3, . . . . . ., akn such that, f(ak1)  f(ak2)  f(ak3)  . . . . . .  f(akn)

  7. Judging the speed of sorting algorithms • Best case - it occurs when the data are already in order. • Average case - it occurs when the data are in random order. • Worst case - it occurs when the data are in reverse order.

  8. Judging the speed sorting algorithm contin. • Does the algorithm exhibit natural or unnatural behavior? • Natural behavior exhibits if the sort works least when the list is already in order, works harder as the list becomes less ordered, and works hardest when a list is in inverse order. • Unnatural behavior exhibits when minimum number of comparisons is needed when data are in reverse order and maximum if data are already in order. • Does it rearrange data with equal keys?

  9. Introduction To Sorting • Only concerned with internal sorting where all records can be sorted in main memory • We are interested in analyzing sorting algorithms for worst-case performance and average performance

  10. Insertion Sort Initial order: SQ SA C7 H8 DK Step 1: SQ SA C7 H8 DK Step 2: C7 SQ SA H8 DK Step 3: C7 DK H8 SQ SA C:Club D:Diamond S:Spade H:Heart

  11. Insertion Sort: Another Example Original 34 8 64 51 32 21 Positions Moved After p=2 8 34 64 51 32 21 1 After p=3 8 34 64 51 32 21 0 After p=4 8 34 51 64 32 21 1 After p=5 8 32 34 51 64 21 3 After p=6 8 21 32 34 51 64 4

  12. Insertion Sort Step 1: sort the first two items of the array. Step 2: insert the third item into its sorted position in relation to the first two items. Step 3: the fourth element is then inserted into the list of three elements Continue: until all items have been sorted

  13. Pseudocode - Insertion Sort for (i = 1; i < n; i++) place data[i] in its proper position; move all elements data[j] > data[i] by one position;

  14. Implementation of the Insertion Sort Template<class genType> void Insertion (genType data[ ], int n) { register int i,j; genType temp; for (i = 1; i < n; i++) temp = data[i]; j = i; while (j > 0 && temp < data[j-1]) { data[j] = data[j-1]; j--; } data[j] = temp; }

  15. Example: sort the array [5 2 3 8 1 ] a[0] a[1] a[2] a[3] a[4] i =1 5 2 3 8 1 j =1,0 temp=2 i=2 2 5 3 8 1 j=2 temp=3 i=3 2 3 5 8 1 j=1,3 temp=8

  16. Insertion sort example contin.1 a[0] a[1] a[2] a[3] a[4] i=4 2 3 5 8 1 j=4 temp=1 i=4 2 3 5 8 j=3 temp=1 i=4 2 3 5 8 j=2 temp=1

  17. Insertion sort example contin.2 a[0] a[1] a[2] a[3] a[4] i=4 2 3 5 8 j=1 temp=1 i=4 2 3 5 8 j=0 temp=1 i=5 1 2 3 5 8 j=0 temp=1

  18. Number of Exchange for each case Insertion Sort Best Case: Cmin = n-1 O(n) Mmin = 2(n-1) Average Case: Cave = 1/4(n^2+n-2) O(n^2) Mave = 1/4(n^2+9n-10) Worst Case: Cmax = 1/2(n^2+n)-1 O(^2) Mmax = 1/2(n^2+3n-4) // C --> Comparisons M --> Moves //

  19. Insertion Sort Running Time • The average case and worst case running times are quadratic. • The best case running time is linear. • The algorithms is quick when the starting array is nearly sorted

  20. Selection Sort Step1: Select the element with the lowest value and exchanges it with the 1st element Step2: From the remaining n-1 elements, the element with the next-lowest value is found and exchanged with the 2nd element. Continue: up to the last two elements.

  21. Pseudocode - Selection Sort For(i =0; i < n-1; i++) select the smallest element among data[i], . . . , data[n-1]; swap it with data[i];

  22. Implementation of the SelectionSort Template<class genType> void Selection(genType data[], int n) { register int i, j, least; for (i = 0; i < n-1; i++) { for (j = i+1, least = i; j < n; j++) if (data[j] < data[least]) least = j; if (i != least) Swap (data[least], data[i]); } }

More Related