# Tag: Sorting

## 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 […]

## Sorting – Address Calculation Sort (Hashing)

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 […]

## Sorting – Quick Sort (Partition Exchange Sort)

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) […]

## Sorting – Merge Sort

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 […]

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 […]

## Sorting – Straight Selection Sort

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 […]

## Sorting – General Selection Sort

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 […]

## Sorting – Insertion sort

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 = […]

## Sorting – Bubble sort

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 –                 […]

## Sorting – Shell sort

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 […]