5.3 Sorting Techniques
Sorting Techniques Sorting is the process of putting the data in alphabetical or numerical order using a key field primary key is the first key by which data in a file is sorted, e.g., area code for a mailing list Secondary key is the second key by which data in a file is sorted within the primary key order. E.g., a mailing list sorted by area code can again be sorted in alphabetical order of name within each area code.
Sorting Techniques Sorting techniques include – 1. Selection Exchange Sort, bubble Sort, Quick Sort, Shell Sort and Heap Sort Best sorting techniques are determined by the number of comparisons and switches that take place for a file of n records in a specific order. Memory space is also a consideration when considering a sorting technique.
The Selection Exchange Sort To sort n records Think of 2 sublists within n records – 1. List of sorted part (S) – 2. List of unsorted part (U) Initially number of elements in S=0 and number of elements in U = n 1. Find the smallest element in unsort part and switch it with the first element of the array.
The Selection Exchange Sort 2. While number of elements in (U) > 1 – Find the smallest element in Unsorted part and switch with first element of Unsorted part. [Once switched this first element of U becomes the last element of S]
The Selection Exchange Sort
int void(main) { int score[6], num_score, I, minpos, j, temp; for (I=0; I score[j]) min = j }/* end of for j */
The Selection Exchange Sort if (min != I) { temp = score[I]; score[I] = score[min]; score[min] = temp; } } /* for I end */ return 0; }/* of main */
Practice Get a piece of paper and copy the algorithm from the screen to a piece of paper and hand in to Richards Create a 1D 6 element array that is populated by user, sort this array using selection sort algorithm, then print the new sorted array to screen.