You are on page 1of 17

List Sort, Table Sort, Sorting Algorithm

Analysis, Hashing Techniques


Mrudang Mehta

List Sorts
When sorting lists with large records, it is

necessary to modify the sorting methods so as to


minimize data movement. ( data movement slows
down the sorting process)
Iterative sort, Merge sort can be modified with the
linked list instead of sequential list
Each record will require an additional link field. So,
Instead of physically moving the record, we
change its link field to reflect the change in the
position of the record in the list.

Rearranging records using


Doubly Linked List
If F is sorted list, then first pointer points to the first record

(node) of the sorted list. All other elements linked to it are


greater or equal to it.

k1

k2

k3

K1<= k2 <= k3 <=kn-1<=kn

kn

void list1(Element *list, const int n, int first)


{// rearrange the sorted chain first so that the records list[1]list[n] are in sorted order.
int prev=0;
for(int current=first; current; current=list[current].linka)
{
//convert chain into doubly list
list[current].linkb=prev;
prev=current;
}
for(int i=1; i<n; i++)// move listfirst to position i while maintaining list
{
if(first !=i){
if(list[i].linka) list[list[i].linka].linkb= first;
list[list[i].linkb].linka=first;
Element a=list[first]; list[first]=list[i]; list[i]=a;
}
first = list[i].linka;
}
}

Example

(a) Linked list of following a List Sort, first=4

(b) Corresponding doubly linked list, first=4

(c) Configuration after first iteration of the for loop of List 1, first=2

(d) Configuration after second iteration, first=6

(e) Configuration after third iteration, first=8

(f) Configuration after forth iteration, first=10

Rearranging Records Using Only


One Link Fields
void List2(Element *list, const int n, int first)
{ // Same function as List1 except that a second link array linkb is not required.
for (int i = 1; i < n; i++)
{// Find correct record to place into ith position. Its index is i as
// records in positions 1, 2, , i 1 are already correctly positioned.
while (first < i) first = list[first].linka;
int q = list[first].linka; // listq is next record in sorted order
if (first != i)
{ // interchange listi and listfirst moving listfirst to its correct spot as
// listfirst has ith smallest key. Also set link from old position of listi to new one
Element t = list[i];
list[i]= list[first]; list[first]=t; list[i].linka = first;
}
first = q;
}
}

Example

(a) Configuration after first iteration of the for loop of List2, first=2

(b) Configuration after second iteration, first=6

(c) Configuration after third iteration, first=8

(d) Configuration after forth iteration, first=10

(e) Configuration after fifth iteration, first=1

Table Sort
The list sort technique is not well suited for quick sort and

heap sort
We can use auxiliary table
At the start of the sort, t[i] i,1 i .nIf the sorting function
requires a swap of a[i] and a[j], then only the table entries
(i.e., t[i] and t[j]) need to be swapped.
At the end of sort, the record with the smallest key is a[t[1]]
and that with the largest key a[t[n]]

Table Sort Program


template <class T>
void Table(T *a, const int n, int *t)
{ // Rearrange a[1:n] to correspond to the sequence a[t[1]], , a[t[1]], n 1.
for (int i = 1; i < n; i++)
if (t[i] != i) { // there is a non-trivial cycle starting at i
T p = a[i];
int j = i;
do {
int k = t[j]; a[j] = a[k]; t[j] = j;
j = k;
} while (t[j] != i)
a[j] = p; // j is position for record p
t[j] = j;
}
}
13

Table Sort Example

(a) Initial configuration

(b) Configuration after rearrangement of first cycle

(c) Configuration after rearrangement of second cycle


14

Summary of Internal Sorting


Comparison of sort methods

Method
Insertion Sort
Heap Sort
Merge Sort
Quick Sort

15

Worst

Average

Average Times for Sort Methods

Times are in milliseconds

Plot of Average Times (milliseconds)


5

Insertion Sort

Heap Sort
Merge Sort

Quick Sort
0
0

500

1000

2000

3000

4000

5000

You might also like