You are on page 1of 9

SORTING

Sorting is the operation of arranging the records


of a table into some sequential order according
to an ordering criterion.
The various sorting techniques are

1) Bubble Sort Selection Sort


2) Selection Sort
3) Insertion Sort
4) Merge Sort
5) Heap Sort
6) Quick Sort
7) Radix Sort

Bubble Sort
It is the simplest sorting technique.
It works by repeatedly stepping through
the list to be sorted, comparing each pair
of adjacent items andswappingthem if
they are in the wrong order.
The pass through the list is repeated until
no swaps are needed, which indicates
that the list is sorted.
The algorithm gets its name from the
way smaller elements "bubble" to the top
of the list or the largest element to the
end of the list as it gradually go ahead.

Bubble sort algorithm

Procedure bubble_sort(a,n)
a vector of n elements
pass pass counter
last position of the last unsorted element
ex - count the number of exchanges made
1) last=n
2) repeat through step 5 for pass=1 to n-1
3) ex=0
4) repeat for i = 1 to last 1
if a[i] > a[i+1] then
swap k[i] and k[i+1]
ex=ex+1
5) if x=0 then
return
else
last=last 1
6) Finish

Bubble sort has worst-case and


average complexity both(n2),
wherenis the number of items being
sorted.
Performance of bubble sort over an
already-sorted list (best-case) isO(n).
Limitation
Time consuming for larger list of
elements

Selection Sort
The algorithm works as follows:
1) Find the minimum value in the list
2)Swap it with the value in the first position
3) Repeat the steps above for the remainder of the list
(starting at the second position and advancing each time)
Effectively, the list is divided into two parts:
- The sublist of items already sorted, which is built up from
left to right and is found at the beginning
- The sublist of items remaining to be sorted, occupying
the remainder of the array.

In Simple words
The selection sort starts from first element and searches
the entire list until it finds the minimum value. The sort
places the minimum value in the first place, selects the
second element and searches the second smallest element
and so on

Procedure selection_sort(a,n)
a vector of n elements
i pass index and the position of the first
element in the vector which is to be examined.
min_index denotes the position of the smallest
element encountered thus far in particular pass
1) Repeat through step 4 for i=1 to n-1
2) min_index= pass
3) Repeat for j= i+1 to n
if a[i] < a[min_index] then
min_index = i
4) if min_index i the n
swap a[i] and a[min_index]
5) finish

Insertion Sort
Insertion sort inserts each item into its proper place in
the final list.
It is one of the simplest methods to sort a list of records
It provides the following advantages
Simple implementation
Efficient for (quite) small data sets
Adaptive, i.e. efficient for data sets that are already
substantially sorted: thetime complexityis O(n+d),
wheredis the number ofinversions
More efficient in practice than most other simple
quadratic, i.e.O(n2) algorithms such asselection sortor
bubble sort; the best case (nearly sorted input) isO(n)
Stable, i.e. does not change the relative order of
elements with equal keys
In-place, i.e. only requires a constant amount O(1) of
additional memory space
Online, i.e. can sort a list as it receives it

Every repetition of insertion sort


removes an element from the input
data, inserting it into the correct
position in the already-sorted list,
until no input elements remain.

Insertion Sort Algorithm


Procedure insertion_sort(a,n)
a vector of n elements
1) repeat through step 3 through 5 for i= 2 to n
2) temp = a[i]
j=i-1
3) repeat while temp < a[j] and j>=1
a[j+1]=a[j]
j=j-1
4) a[j]=temp
5) Finish

You might also like