You are on page 1of 9

SORTING

Sorting allow as efficient arrangement of elements within a given data structure. It is the way in which
elements are organized systematically for some purpose. Take an example of dictionary in which words are
arranged in alphabetical order so that one can easily locate a desired word. Consider telephone in which
subscribers names are arranged in alphabetic order so that one can easily find phone number of particular
person. Similarly, a librarian needs to arrange books in some order so that a particular book can be easily
found. There are many such applications of sorting techniques in the real-world.
Sorting: Sorting is process of arranging the elements in a particular order. The order may be ascending
order or descending order. The advantage of sorting is effective data accessing. Types of sorting: There are 2
types of sorting.
1. Internal sorting
2. External sorting
Internal sorting: If all the elements (records) to be sorted are in the main memory then such a sorting is
called internal sorting.
External sorting: If some of the elements (records) to be sorted are in the secondary storage or disk such a
sorting is called External sorting. Types of sorting techniques:
1. Bubble sort (Exchange sort or Sinking sort).
2. Selection sort.
3. Insertion sort.
4. Quick sort.
5. Merge sort.
6. Shell sort
7. Radix sort.
8. Heap sort.
Efficiency of a sorting technique:
There are number of sorting techniques available to sort a given array of data items. Each sorting technique
has its own advantages and disadvantages. Different techniques are useful in different applications.
Therefore, considerable care should be taken before selecting a sorting technique. Similarly, a suitable
sorting technique should be selected for given input array of data items, otherwise sorting may become
complex or result to poor performance.
Selecting a sophisticated sorting technique for small input set may not serve the purpose. Similarly selecting
a complex sorting technique for complex input set will result in poor performance.
There are 3 most important factors are counted while selecting a sorting technique, which are.
1. Coding time: The amount of time invested in writing full length sorting program.
2. Execution time (Time complicity): The amounts of time required execute the sorting program. This
normally frequency of execution of statements in a program i.e. number of times statements are executed.
3. Memory requirement (Space complicity): The amount of memory required to store the entire sorting
program in main memory while execution.
BUBBLE SORT
The bubble sort gets its name because as array elements are sorted they gradually bubble to their proper
positions.
A bubble sort will compare two adjacent element of an array and will swap them if they are out of order.
The compare starts with the first and second element. After that it will compare the second with the third
element and so on. The process continues until the end of the list is reached.
When the end of the array is reached, the bubble sort algorithm will return to element one and starts the
process all over again. So, when will the bubble sort algorithm stop? The bubble sort algorithm knows when
its finish when there are no more swaps.
As you can see in the source below, the bubble sort algorithm is easy to program. But the bubble sort
algorithm is slower than other sorting algorithms. Because the sort always needs a final extra pass to check
to see that there are no more swaps are made
BCK/CS DEPT/DS/AMS

SORTING
Algorithm 1: Bubblesort(A, N) - Here A is a linear array with N elements. I, J are the iteration
variables and TEMP is temporary variable to store the swapped value temporarily. This algorithm
sorts the array elements using bubble sort technique.
Step 1: Start
Step 2: For I=0 To N-1
For J=0 To N-1-I
if(A[J]>A[J+1]) then
Set TEMP=A[J]
Set A[J]=A[J+1]
Set A[J+1] =TEMP
[End of If]
[End of for loop]
[End of for loop]
Step 3: Stop
Example
To understand logic of Bubble Sorting, lets take random numbers:
6 3 7 1 4 5 2
First iteration
When i=0 j=0 Compare the first two elements '6' and '3'. swap is necessary because '6' is greater than '3'.
6 3 7 1 4 5 2
When i=0 j=1 Compare second and third elements '6' and '7'. swap is not necessary because '6' is lesser than
'7'.
3 6 7 1 4 5 2
When i=0 j=2 Compare third and fourth elements '7' and '1'. swap is necessary because '7' is greater than '1'.
3 6 7 1 4 5 2
When i=0 j=3 Compare fourth and fifth elements '7' and '4'. swap is necessary because '7' is greater than '4'.
3 6 1 7 4 5 2
When i=0 j=4 Compare fifth and sixth elements '7' and '5'. swap is necessary because '7' is greater than '5'.
3 6 1 4 7 5 2
When i=0 j=5 Compare sixth and seventh elements '7' and '2'. swap is necessary because '7' is greater than
'2'.
3 6 1 4 5 7 2
After the Swap we get an array as follows
3 6 1 4 5 2 7
Second iteration
When i=1 j=0 Compare the first two elements '3' and '6'. swap is not necessary because '3' is lesser than '6'.
3 6 1 4 5 2 7
When i=1 j=1 Compare second and third elements '6' and '1'. swap is necessary because '6' is greater than '1'.
3 6 1 4 5 2 7
When i=1 j=2 Compare third and fourth elements '6' and '4'. swap is necessary because '6' is greater than '4'.
3 1 6 4 5 2 7
When i=1 j=3 Compare fourth and fifth elements '6' and '5'. swap is necessary because '6' is greater than '5'.
3 1 4 6 5 2 7
When i=1 j=4 Compare fifth and sixth elements '6' and '2'. swap is necessary because '6' is greater than '2'.
3 1 4 5 6 2 7
After the Swap we get an array as follows
3 1 4 5 2 6 7
Third iteration
When i=2 j=0 Compare the first two elements '3' and '1'. swap is necessary because '3' is lesser than '1'.
BCK/CS DEPT/DS/AMS

SORTING
3 1 4 5 2 6 7
When i=2 j=1 Compare second and third elements '3' and '4'. swap is not necessary because '3' is lesser than
'4'.
1 3 4 5 2 6 7
When i=2 j=2 Compare third and fourth elements '4' and '5'. swap is not necessary because '4' is lesser than
'5'.
1 3 4 5 2 6 7
When i=2 j=3 Compare fourth and fifth elements '5' and '2'. swap is necessary because '5' is greater than '2'.
1 3 4 5 2 6 7
After the Swap we get an array as follows
1 3 4 2 5 6 7
Four iteration
When i=3 j=0 Compare the first two elements '1' and '3'. swap is not necessary because '1' is lesser than '3'.
1 3 4 2 5 6 7
When i=3 j=1 Compare second and third elements '3' and '4'. swap is not necessary because '3' is lesser than
'4'.
1 3 4 2 5 6 7
When i=3 j=2 Compare third and fourth elements '4' and '2'. swap is necessary because '4' is greater than '2'.
1 3 4 2 5 6 7
After the Swap we get an array as follows
1 3 2 4 5 6 7
Five iteration
When i=4 j=0 Compare the first two elements '1' and '3'. swap is not necessary because '1' is lesser than '3'.
1 3 2 4 5 6 7
When i=4 j=1 Compare second and third elements '3' and '2'. swap is necessary because '3' is greater than '2'.
1 3 2 4 5 6 7
After the Swap we get an array as follows
1 2 3 4 5 6 7
Six iteration
When i=5 j=0 Compare the first two elements '1' and '2'. swap is not necessary because '1' is lesser than '2'.
1 2 3 4 5 6 7
We get an array as follows
1 2 3 4 5 6 7
Seven iteration
Since there is no element to compare the final array will be as follows
1 2 3 4 5 6 7
Advantage
1. It's Simple.
Disadvantages
1. Very time-consuming, not recommended for long searches.
INSERTION SORT
The Insertion Sort works by picking an element, figuring out where it should go, then putting it there.
Insertion sort algorithm somewhat resembles selection sort. Array is imaginary divided into two parts sorted one and unsorted one. At the beginning, sorted part contains first element of the array and unsorted
one contains the rest. At every step, algorithm takes first element in the unsorted part and inserts it to the
right place of the sorted one. When unsorted part becomes empty, algorithm stops. Sketchy, insertion sort
algorithm step looks like this:

BCK/CS DEPT/DS/AMS

SORTING

becomes

Algorithm 2: Insertion sort (A, N) - Here A is a linear array with N elements. I, J are the iteration
variables and TEMP is temporary variable to store the swapped value temporarily. This algorithm
sorts the array elements using Insertion sort technique.
Step 1: Start
Step 2: For I=0 to N-1
Set TEMP=A[I]
For J=I-1 to 0
If(A[J]>TEMP) then
Set A[J+1] =A[J]
Else
Exit
[End of If]
[End of for]
Set A[J+1] =TEMP
[End of for loop]
Step 3: Stop
Example
Starting near the top of the array in Figure 2-1(a), we extract the 3. Then the above elements are shifted
down until we find the correct place to insert the 3. This process repeats in Figure 2-1(b) with the next
number. Finally, in Figure 2-1(c), we complete the sort by inserting 2 in the correct place.

Figure 2-1: Insertion Sort

BCK/CS DEPT/DS/AMS

SORTING
Advantages
1. They efficient for small sets of data
2.They are simply implemented.
3. Insertion sorts only pass through the array only once.
4. They are adaptive; efficient for data sets that are already sorted.
Disadvantage
1. It's less efficient on larger list and arrays.
SELECTION SORT
The idea of the selection sort is to find the smallest element in the list and exchange it with the element in
the first position. Then, find the second smallest element and exchange it with the element in the second
position, and so on until the entire array is sorted.
STEP 1: Start
STEP2: Repeat for I=0 To N-2
Set SMALL=A[I]
Set POS=I
Repeat for J=I+1 To N-1
if(A[J]<SMALL) then
Set SMALL=A[J], POS=J
[End of IF]
[End of Loop]
Set A[POS]=A[I], A[I]=SMALL
[End of Loop]
Step 3: Exit
Example
First, we give the computer a list of unsorted numbers and store them in an array of memory cells.

To begin the sort, the computer divides the sorted and unsorted sections of the list by placing a marker
before the first number. To sort the numbers, the computer will repeatedly search the unsorted section for
the smallest number, swap this number with the first number in the unsorted section, and update the sort
marker.

To find the smallest number in the unsorted section, the computer must make six comparisons: (7 < 8),
(7 > 5), (5 > 2), (2 < 4), (2 < 6), and (2 > 3). After these comparisons, the computer knows that 2 is the
smallest number, so it swaps this number with 7, the first number in the unsorted section, and advances the
sort marker.

BCK/CS DEPT/DS/AMS

SORTING
Now five more comparisons are needed to find the smallest number in the unsorted section: (8 > 5),
(5 < 7), (5 > 4), (4 < 6), and (4 > 3). After these comparisons, the computer swaps 3, the smallest number in
the unsorted section, with 8, the first number in the unsorted section, and advances the sort marker.

This time four comparisons are needed to determine that 4 is the smallest number in the unsorted section:
(5 < 7), (5 > 4), (4 < 6), and (4 < 8). After these comparisons, the computer swaps 4 with 5 and then
advances the sort marker.

After three more comparisons, the computer identifies 5 as the smallest unsorted number: (7 > 5), (5 < 6),
and (5 < 8). Then the computer swaps 5 with 7 and advances the sort marker.

This time only two comparisons are needed to determine that 6 is the smallest number: (7 > 6) and
(6 < 8). After these two comparisons, the computer swaps 6 with 7 and then advances the sort marker.

Now we only need a single comparison to find the right position for 7: (7 < 8). Since 7 is the smallest
number and it is also the first number in the unsorted section, the computer does not need to swap this
number. It only needs to advance the sort marker. Now there is only one number in the unsorted section, so
the list of numbers is sorted and the Selection Sort algorithm is complete.

Advantages
The main advantage of the selection sort is that it performs well on a small list.
2.They are simply implemented.
Disadvantages
The selection sort is its poor efficiency when dealing with a huge list of items.

BCK/CS DEPT/DS/AMS

SORTING
MERGE SORT
The merge sort algorithm is based on the "divide & conquer" strategy. Here, we divide the given array to
be sorted into two halves, sort these two sub-arrays separately, and then combine (merge) these sorted subarrays to produce solution to the original problem. For sorting the sub-arrays, we use recursion
Algorithm 3: mergesort(A, LOW, HIGH)- Let A be an Linear Array with N elements. Low and High is
the lower bound and upper bound in the array.
mergesort(A, LOW, HIGH) this function will call the mergesort function recursively to sort the array
element also call the function merge to merge the divided elements.
Step 1: Start
Step 2: Set MID=(LOW+HIGH)/2;
If(LOW<HIGH) then
Call mergesort(A, LOW, MID)
Call mergesort(A, MID+1,HIGH)
Call merge(A, LOW, MID, HIGH)
[End of If]
Step 3: Exit
merge (A, LOW, MID, HIGH) - the following function will merge the element in the sorted order
Step 1: Start
Step 2: Set I=LOW
Set J=MID+1
Set K=LOW
Repeat While I<=MID And J<=HIGH
If(A[I]>A[J]) then
Set C[K]=A[J]
Set C=K+1
Set J=J+1
else
Set C[K]=A[I]
Set I=I+1
Set K=K+1
[End of If]
[End of While loop]
Repeat While(I<=MID)
Set C[K]=A[I]
Set I=I+1
Set K=K+1
[End of While loop]
Repeat While(J<=HIGH)
Set C[K]=A[J]
Set K=K+1
Set J=J+1
[End of While Loop]
For I=LOW To HIGH
Set A[I]=C[I]
[End of For]
Step 3: Return
The full problem is to sort an entire array. Let's say that a sub problem is to sort a subarray. In particular,
we'll think of a sub problem as sorting the subarray starting at index p and going through index r. It will be
convenient to have a notation for a subarray, so let's say that array [p to r] denotes this subarray of array. In
BCK/CS DEPT/DS/AMS

SORTING
terms of our notation, for an array of n elements, we can say that the original problem is to sort array [0 To
n-1].
Here's how merge sort uses divide-and-conquer:
1. Divide by finding the number q of the position midway between p and r. Do this step the same way
we found the midpoint in binary search: add p and r, divide by 2, and round down.
2. Conquer by recursively sorting the subarrays in each of the two sub problems created by the divide
step. That is, recursively sort the subarray array [p To q] and recursively sort the subarray array [q+1
To r].
3. Combine by merging the two sorted subarrays back into the single sorted subarray array [p To r].
Let's see an example. Let's start with array holding [14, 7, 3, 12, 9, 11, 6, 2], so that the first subarray is
actually the full array, array [0 To 7] (p=0and r=7). This subarray has at least two elements, and so it's not a
base case.
In the divide step, we compute q=3
The conquer step has us sort the two subarrays array[0 To 3], which contains [14, 7, 3, 12], and
array[4 To 7], which contains [9, 11, 6, 2]. When we come back from the conquer step, each of the
two subarrays is sorted: array[0 TO 3] contains [3, 7, 12, 14] and array[4To 7] contains [2, 6, 9, 11],
so that the full array is [3, 7, 12, 14, 2, 6, 9, 11].
Finally, the combine step merges the two sorted subarrays in the first half and the second half,
producing the final sorted array [2, 3, 6, 7, 9, 11, 12, 14].
How did the subarray array[0 TO 3] become sorted? The same way. It has more than two elements, and so
it's not a base case. With p=0 and r=3, compute q=1, recursively sort array[0 To 1] ([14, 7]) and array[2 To
3] ([3, 12]), resulting in array[0 To 3] containing [7, 14, 3, 12], and merge the first half with the second half,
producing [3, 7, 12, 14].
How did the subarray array[0 To 1] become sorted? With p=0and r=1, compute q=0, recursively sort array[0
To 0] ([14]) and array[1 To 1] ([7]), resulting in array[0 To 1] still containing [14, 7], and merge the first
half with the second half, producing [7, 14].
The subarrays array[0 To 0] and array[1 TO 1] are base cases, since each contains fewer than two elements.
Here is how the entire merge sort algorithm unfolds:

BCK/CS DEPT/DS/AMS

SORTING

BCK/CS DEPT/DS/AMS

You might also like