## Sorting on multiple keys

Sorting on multiple keys The sorting algorithm may be applied on multiple keys such that if first field contains duplicate values, then sorting is done on a secondary field and so on. However, if the first field contains unique values, then sorting is not applied on secondary field. Say for example: The input for the combination {studentname, feeamt} { (xyz,3000), (abc,1000), (xyz, 1000), (abc,3000)}; will result in the following output:…

Read more...## Sorting – Address Calculation Sort (Hashing)

are closed

Sorting – Address Calculation Sort (Hashing) In this method a function f is applied to each key. The result of this function determines into which of the several subfiles the record is to be placed. The function should have the property that: if x <= y , f (x) <= f (y), Such a function is called order preserving. An item is placed into a subfile in correct sequence by…

Read more...## Sorting – Quick Sort (Partition Exchange Sort)

are closed

Sorting – Quick Sort (Partition Exchange Sort) Suppose x be an array, n is the number of elements. Choose an element from a specific position within the array. Array x is partitioned so that y is placed into position j, and the following conditions hold: 1) Each of the elements in position 0 through (j-1) is less than or equal to y. 2) Each of the elements in position (j+1)…

Read more...## Sorting – Merge Sort

are closed

Sorting – Merge Sort Merging:- Merging is the process of combining two or more sorted files into a third sorted file. An example of a routine that accepts two sorted arrays a and b of n1 and n2 elements respectively and merges them into a third array c containing n3 (n1+n2) elements. Steps for Merge Sort: Divide the file n subfiles of size 1 and merge adjacent pair of files….

Read more...## Sorting – Radix Sort

are closed

Sorting – Radix Sort Radix Sort sorts the number in scans equal to the number of digits of maximum number. eg. 45, 3, 235, 89, 150, then maximum scans = 3. Thus radix sort operates three times. Radix sort uses 10 queues to sort the given numbers. Begin with least significant digit, ending with most significant digit. Fetch a number from the array and place it in one of the…

Read more...## Sorting – Straight Selection Sort

are closed

Sorting – Straight Selection Sort Begin from the first element, taking i=0 to n-1. Find the minimum number in the first scan. Interchange ith element with the minimum number. Start searching minimum element again from i+1 to n. The number of scans is equal to the number of elements. Stop scan when the last element remains to be replaced with min. Display the output which is now sorted. Example: Though…

Read more...## Sorting – General Selection Sort

are closed

Sorting – General Selection Sort Steps: Fetch the numbers to be sorted in an array. Push these numbers one by one to a priority queue. (Any priority-lower or higher may be considered). Case 1: Lower values with high priority for ascending order. Case 2: Higher values with high priority for descending order. 3. Pop all the elements from the priority queue to the array one by one. 4. Display the…

Read more...## Sorting – Insertion sort

are closed

Insertion Sort Sorts a set of records by comparing the next record with all the previous elements. Compare next element with previous. If next element is smaller than previous element, then swap both the elements. i.e. if(arr[j]>arr[i]) swap(arr[j],arr[i); Example: The loop may be written as follows: for I = 2 to n for j = I-1 to >=1 if(arr[I] < arr[j]) swap(arr[i],arr[j]); Insertion sort is considered best if most of…

Read more...## Sorting – Bubble sort

are closed

Bubble sort: Two consecutive elements are compared. It sorts the elements in right to left fashion. Thus, (n-i) comparisons are made. Example: Frequency: i j number of times ——————————————————————————————— 1 1 to n-i n-1 2 1 to n-i n-2 3 1 to n-i n-3 – …

Read more...## Sorting – Shell sort

are closed

Shell Sort: Shell sort is quite similar to that of insertion sort with the only difference that in shell sort, higher values of k are considered. Whereas insertion sort assumes k to be 1. If k=4, then every 4th element is compared, if it is 3 then every 3rd element is compared. Thus k=m means every mth element gets compared with each other and is swapped. The value of k…

Read more...
are closed