• No results found

Sorting Techniques

In document Data Structures (Page 34-39)

Sorting:-Process of re-arranging a given set of elements in a specific (ascending or  descending) order.

Uses:-Used as an aid in searching.

As a means for matching entries in files.

Applications in operations research and job scheduling.

Comparison

No. of records key size Sorting method   Less --- Selection sort or Bubble sort

More short radix sort

More long quick sort or heap sort or merge sort If records are initially almost sorted, avoid quick sort method.

Internal sorting

Methods to be used when the file to be sorted is small enough so that the entire sort can be carried out in main memory.

Requires excessive data movement.

:

Insertion sort Quick sort Heap sort Radix sort Limitation

Slows down the sorting process when records are large.

External sorting

They are the methods that can be used on larger files.

Most popular method is Merge sort.

Merge sort method

It consists of two distinct phases:

1. Segments of the input file are sorted using an internal sort method.

The sorted segments (runs) are written out onto external storage as they are generated.

2. The runs generated in phase 1 are merged together following the merge tree pattern until only one run is left.

Exchange / Bubble Sort

1. Repeat through step 4 a total of n-1 times.

2. Repeat step 3 for elements in unsorted portion of the array.

3. If the current element in the array > next element in the array then exchange elements.

4. If no exchanges were made then return else reduce the size of the unsorted array by one.

Procedure Bubble-Sort (K, N) K Array of N elements PASS Pass counter variable

LAST Position of the last unsorted element

I Index

EXCHS Variable to count the number of exchanges made on any pass.

1. [Initialise]

LAST

N-1;

PASS

1;

2. [Initialise exchanges counter for this pass]

EXCHS

0

3. [Perform pair-wise comparisons on unsorted elements].

Repeat for I = 1, 2, …, LAST if K[I] > K[I+1] then

K[I]

K[I+1]

EXCHS

EXCHS + 1 4. [Any exchanges made on this pass?]

if EXCHS = 0 then return

else LAST

LAST – 1; (reduce size of unsorted list) PASS

PASS + 1;

if PASS = N – 1 return else go to Step 2.

The time complexity for bubble sort method is:

Best case O(n)

Average Case O(n2) Worst Case O(n2) Selection Sort

Beginning with the first element in the array, a search is performed to locate the element which has the smallest key. When this element is found, it is interchanged with the first element in the array. This interchange places the element with the smallest key, in the first position of the array.

A search for the second element with smallest key is then carried out by examining the keys of the elements from the second element onwards. The element, which has the smallest key, is interchanged with the element located in the record position of the array. The process continues until all records have been sorted in ascending order.

Procedure Selection_Sort (K, N)

K Array of N elements PASS Pass index variable

MIN_INDEX Position variable of the smallest element

I Index

1. PASS = 1

2. [Initialise minimum index]

MIN_INDEX

PASS

3. [Make a pass and obtain element with smallest value]

repeat for I = PASS + 1, PASS + 2, …, N if K[I] < K[MIN-INDEX] then

MIN-INDEX

I 4. [Exchange elements]

if MIN-INDEX

PASS then

K [PASS]

K[MIN-INDEX]

PASS

PASS + 1;

if PASS

N then go to Step 2 else return;

The time complexity for Selection Sort Method is:

Best Case O(n2) Average Case O(n2) Lowest Case O(n2) Quick / Partition Exchange Sort

At each step, a particular element is placed in its final position within the list. All elements, which precede this element, have smaller  keys/values, while all elements that follow it have larger keys. This technique partitions the list into two sub-lists.

The same process can then be applied to each of these sub-lists and repeated until all elements are placed in their final positions. It performs well on larger list of elements.

Procedure Quick_Sort(K, LB, UB) K Array of n elements

LB lower bound of the current sub-list.

UB upper bound of the current sub-list.

I, J Index variable

KEY key value

FLAG logical variable 1. [Initialise]

FLAG

true 2. [Perform Sort]

if LB < UB then I

LB J

UB + 1 KEY

K[LB]

Repeat while (FLAG) I

I + 1

Repeat while K[I] < KEY (Scan the keys from left to right)

I

I + 1 J

J – 1

Repeat while K[J] > KEY (Scan the keys from right to left)

J

J – 1 if I < J then

K[I]

K[J] (interchange elements) else FLAG

false

K[LB]

K[J] (interchange elements)

Call Quick_Sort (K, LB, J – 1) (Sort first sub-list) Call Quick_Sort (K, J + 1, UB) (Sort second sub-list) 3. [Finished]

Return

The time complexity of Quick Sort method is:

Best Case O(nlog2n)

Average Case O(nlog2n) Worst Case O(n2)

Heap

Sort:-Heap sort method consists of two phases, namely, construction phase and a traversal phase. The construction Phase consists of successively inserting a new element in a tree structure. The tree obtained from this phase can be traversed in inorder. In a list of elements, heap satisfies the property K j

Ki for 2

j

n and i =

 j/2

.

The binary tree is allocated sequentially such that the indices of the left and right sons (if they exist) of element i are 2i and 2i + 1, respectively i.e. the index of the parent of element j if (if it exists) is

 j/2

. The element with the largest key is at the root of the tree (also called the top of  the heap).

The starting point is to have a heap initially with one element tree and then, insert a new element into the existing heap such that a new heap is formed after performing the insertion. Insertions are performed repeatedly until all elements in the original list form a heap.

Procedure CREATE-HEAP (K, N) K Array of N elements Q, I, J Index variable

KEY key of the element 1. [Build Heap]

repeat thru step 7 for Q = 2, 3, …, N 2. [Initialise construction phase]

I

Q

KEY

K[Q]

3. [Obtain parent of new element]

J

TRUNC (I / 2)

4. [Place new element in existing heap]

repeat thru step 6 while I > 1 and KEY > K[J]

5. [Interchange element]

K[I]

K[J]

6. [Obtain next parent]

I

J

J

TRUNC (I / 2) if J < 1 then

J

1

7. [Copy new element into its proper place]

K[I]

KEY 8. [Finished]

Return

Procedure Heap-Sort (K, N) K Array of N elements Q Pass Index variable

I Index variable

J Index variable KEY Integer variable 1. [Create the initial heap]

Call CREATE_HEAP (K, N) 2. [Perform Sort]

repeat thru step 10 for Q = N, N – 1, …, 2 3. [Exchange element]

K[1]

K[Q]

4. [Initialise pass]

I

1

KEY

K[1]

J

2

5. [Obtain index of largest son of new element]

if J + 1 < Q then

if K[J+1] > K[J] then J

J +1 6. [Reconstruct the new heap]

repeat thru step 10 while (j

Q – 1 and K[J] > KEY) 7. [interchange element]

K[I]

K [J]

8. [Obtain next left son]

I

J J

2 * I

9. [Obtain index of next largest son]

if J + 1 < Q then

if K[J+1] > K [J] then J

J +1 else if J > N then J

N

10. [Copy element into its proper place]

K[I]

KEY 11. [Finished]

Return

The time complexity of heap sort is:

Best Case O(nlog2n) Average Case O(nlog2n) Worst Case O(n2)

Algorithm Average Case Worst case Space usage

Selection_sort n2/4 n2/4 In space

Bubble_sort n2/4 n2/2 In space

Merge_sort O(nlog2n) O(nlog2n) Extra n entries.

Quick_sort O(nlog2n) n2/2 Extra log2n entries

Heap-sort O(nlog2n) O(nlog2n) In space

In document Data Structures (Page 34-39)

Related documents