Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sorting Techniques: 1. Explain in Detail About Sorting and Different Types of Sorting Techniques

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

SORTING TECHNIQUES

1. Explain in detail about sorting and different types of sorting techniques


Sorting is a technique to rearrange the elements of a list in ascending or descending order, which can be
numerical, lexicographical, or any user-defined order. Sorting is a process through which the data is
arranged in ascending or descending order. Sorting can be classified in two types;

Internal Sorts:- This method uses only the primary memory during sorting process. All data items
are held in main memory and no secondary memory is required this sorting process. If all the data that
is to be sorted can be accommodated at a time in memory is called internal sorting. There is a limitation
for internal sorts; they can only process relatively small lists due to memory constraints. There are 3 types
of internal sorts.

(i) SELECTION SORT :- Ex:- Selection sort algorithm, Heap Sort algorithm

(ii) INSERTION SORT :- Ex:- Insertion sort algorithm, Shell Sort algorithm

(iii) EXCHANGE SORT :- Ex:- Bubble Sort Algorithm, Quick sort algorithm

External Sorts:- Sorting large amount of data requires external or secondary memory. This process
uses external memory such as HDD, to store the data which is not fit into the main memory. So, primary
memory holds the currently being sorted data only. All external sorts are based on process of merging.
Different parts of data are sorted separately and merged together. Ex:- Merge Sort

2. Explain the algorithm for bubble sort and give a suitable example. (OR) Explain the algorithm
for exchange sort with a suitable example.
In bubble sort method the list is divided into two sub-lists sorted and unsorted. The smallest element is
bubbled from unsorted sub-list. After moving the smallest element the imaginary wall moves one element
ahead. The bubble sort was originally written to bubble up the highest element in the list. But there is no
difference whether highest / lowest element is bubbled. This method is easy to understand but time
consuming. In this type, two successive elements are compared and swapping is done. Thus, step-by-step
entire array elements are checked. Given a list of n elements the bubble sort requires up to n-1 passes
to sort the data.

Algorithm for Bubble Sort: Bubble_Sort ( A [ ] , N )

Step 1 : Repeat For P = 1 to N 1 Begin


Step 2 : Repeat For J = 1 to N P Begin
Step 3 : If ( A [ J ] < A [ J 1 ] )
Swap ( A [ J ] , A [ J 1 ] ) End For
End For
Step 4 : Exit

-MALCOLM MONSERRATE
SORTING TECHNIQUES

Example:

Ex:- A list of unsorted elements are: 10 47 12 54 19 23

(Bubble up for highest value shown here)

A list of sorted elements now : 54 47 23 19 12 10

3. Explain the algorithm for insertion sort and give a suitable example.
Both the selection and bubble sorts exchange elements. But insertion sort does not exchange elements.
In insertion sort the element is inserted at an appropriate place similar to card insertion. Here the
list is divided into two parts sorted and unsorted sub-lists. In each pass, the first element of unsorted sub
list is picked up and moved into the sorted sub list by inserting it in suitable position. Suppose we have
n elements, we need n-1 passes to sort the elements. Insertion sort works this way:

It works the way you might sort a hand of playing cards:

1. We start with an empty left hand [sorted array] and the cards face down on the table [unsorted
array].
2. Then remove one card [key] at a time from the table [unsorted array], and insert it into the
correct position in the left hand [sorted array].
3. To find the correct position for the card, we compare it with each of the cards already in the
hand, from right to left.

INSERTION_SORT (A)

1. FOR j 2 TO length[A]
2. DO key A[j]
3. {Put A[j] into the sorted sequence A[1 . . j 1]}
4. ij1

-MALCOLM MONSERRATE
SORTING TECHNIQUES

5. WHILE i > 0 and A[i] > key


6. DO A[i +1] A[i]
7. ii1
8. A[i + 1] key

Read the figure row by row. Elements to the left of A[j] that are greater than A[j] move one
position to the right, and A[j] moves into the evacuated position.

Ex:- A list of unsorted elements are: 78 23 45 8 32 36 . The results of insertion


sort for each pass is as follows:-

A list of sorted elements now : 8 23 32 36 45 78

4. Explain the algorithm for selection sort and give a suitable example.

In selection sort the list is divided into two sub-lists sorted and unsorted. These two lists are divided by
imaginary wall. We find a smallest element from unsorted sub-list and swap it to the beginning. And the
wall moves one element ahead, as the sorted list is increases and unsorted list is decreases.

Assume that we have a list on n elements. By applying selection sort, the first element is compared with
all remaining (n-1) elements. The smallest element is placed at the first location. Again, the second

-MALCOLM MONSERRATE
SORTING TECHNIQUES

element is compared with remaining (n-1) elements. At the time of comparison, the smaller element is
swapped with larger element. Similarly, entire array is checked for smallest element and then swapping
is done accordingly. Here we need n-1 passes or iterations to completely rearrange the data.

Algorithm: Selection_Sort ( A [ ] , N )

Step 1 : Repeat For K = 0 to N 2 Begin

Step 2 : Set POS = K

Step 3 : Repeat for J = K + 1 to N 1 Begin

If A[ J ] < A [ POS ]

Set POS = J

End For

Step 5 : Swap A [ K ] with A [ POS ]

End For

Step 6 : Exit
Ex:- A list of unsorted elements are: 23 78 45 8 32 5

A list of sorted elements now : 8 23 32 45 56 78

5. Explain the algorithm for QUICK sort ( partition exchange sort) and give a suitable example.
Quick sort is based on partition. It is also known as partition exchange sorting. The basic
concept of quick sort process is pick one element from an array and rearranges the remaining
elements around it. This element divides the main list into two sub lists. This chosen element is
called pivot. Once pivot is chosen, then it shifts all the elements less than pivot to left of value

-MALCOLM MONSERRATE
SORTING TECHNIQUES

pivot and all the elements greater than pivot are shifted to the right side. This procedure of
choosing pivot and partition the list is applied recursively until sub-lists consisting of only one
element.
Ex:- A list of unsorted elements are: 8 3 2 11 5 14 0 2 9 4
20

Algorithm for quick sort:

It is also known as partition exchange sort. It was invented by CAR Hoare. It is based on partition.
The basic concept of quick sort process is pick one element from an array and rearranges the
remaining elements around it. This element divides the main list into two sub lists. This chosen
element is called pivot. Once pivot is chosen, then it shifts all the elements less than pivot to left of value
pivot and all the elements greater than pivot are shifted to the right side. This procedure of choosing pivot
and partition the list is applied recursively until sub-lists consisting of only one element.

quicksort(q)

varlist less, pivotList, greater

if length(q) 1

return q

select a pivot value pivot from q

for each x in q except the pivot element

if x < pivot then add x to less

if x pivot then add x to greater

add pivot to pivotList

return concatenate(quicksort(less), pivotList, quicksort(greater))

-MALCOLM MONSERRATE
SORTING TECHNIQUES

Time Complexity of Quick sort:

Best case : O (n log n)

Average case : O (n log n)

Worst case : O (n2)

Advantages of quick sort

1. This is faster sorting method among all.


2. Its efficiency is also relatively good.
3. It requires relatively small amount of memory.

Disadvantages of quick sort:

1. It is complex method of sorting so, it is little hard to implement than other sorting methods.

6. Explain the algorithm for Merge sort and give a suitable example.
The basic concept of merge sort is divides the list into two smaller sub-lists of approximately equal
size. Recursively repeat this procedure till only one element is left in the sub-list.After this, various sorted
sub-lists are merged to form sorted parent list. This process goes on recursively till the original sorted list
arrived.

Algorithm for merge sort:

Merge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower
order of growth than insertion sort. Since we are dealing with sub-problems, we state each sub-
problem as sorting a sub-array A[p .. r]. Initially, p = 1 and r = n, but these values change as we
recurse through sub-problems.

To sort A[p .. r]:

1. Divide Step

If a given array A has zero or one element, simply return; it is already sorted. Otherwise,
split A[p .. r] into two sub-arrays A[p .. q] and A[q + 1 .. r], each containing about half of
the elements of A[p .. r]. That is, q is the halfway point of A[p .. r].

2. Conquer Step

Conquer by recursively sorting the two sub-arrays A[p .. q] and A[q + 1 .. r].

3. Combine Step

Combine the elements back in A[p .. r] by merging the two sorted sub-arrays A[p .. q]
and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure
MERGE (A, p, q, r).

-MALCOLM MONSERRATE
SORTING TECHNIQUES

Note that the recursion bottoms out when the sub-array has just one element, so that it is trivially
sorted.
To sort the entire sequence A[1 .. n], make the initial call to the procedure MERGE-SORT (A, 1, n).

MERGE-SORT (A, p, r
1. IF p < r // Check for base case
2. THEN q = FLOOR[(p + r)/2] // Divide step
3. MERGE (A, p, q) // Conquer step.
4. MERGE (A, q + 1, r) // Conquer step.
5. MERGE (A, p, q, r) // Conquer step.

Ex:- A list of unsorted elements are: 39 9 81 45 90 27 72 18

Sorted elements are: 9 18 27 39 45 72 81 90

-MALCOLM MONSERRATE

You might also like