0% found this document useful (0 votes)
12 views12 pages

05 - DS - Sorting-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

05 - DS - Sorting-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Sorting Techniques

Sorting
 Sorting is the process of arranging a set of data in certain order.

 Internal Sorting
Deals with sorting the data held in memory of the computer

 External Sorting
Deals with sorting the data stored in data files, it is used when
volume of data is very large and cannot be held in computers main
memory

Some of the sorting algorithms are


◦ Selection sort
◦ Bubble sort
◦ Insertion sort
◦ Quick sort
◦ Radix sort
◦ Merge sort
◦ Bucket sort
◦ Tree sort 11/12/2024 2
Selection Sort

Process Flow:
Read array size.
Read an array of values.
Sort the given array.
Print sorted values.

11/12/2024 3
Selection Sort
 Finding smallest in each iteration

Begin
set small = a[k-1]
set loc = k-1
for j = k to (n-1) by 1 do
if (a[j]< small) then
set small = a[j]
set loc = j
end if
end for
End

11/12/2024 4
Void SelectionSort(int a[], int n) Selection Sort

{
int temp, small, loc, I, j;
For (i=1;i<=(n-1);i++)
{
small = a[i-1];
Loc=i-1;
For (j=i; j<=(n-1);j++)
{
If(a[j]<small)
{
Small=a[j];
Loc=j;
}
}
If(loc !=(i-1) {
Analysis
temp=a[i-1];
a[i-1]=a[loc];
F(n) = (n-1) +(n-2) +………+/3 +2+1
a[loc]= temp;
= n (n-1 ) / 2
} = O (n2 )
} 11/12/2024 5
Bubble Sort
 Bubble sort is a simple and well-known
sorting algorithm.
 Bubble sort belongs to O(n2) sorting
algorithms, which makes it quite inefficient
for sorting large data volumes.
 Bubble sort is stable and adaptive.
 Algorithm

◦ Compare each pair of adjacent elements from the


beginning of an array and, if they are in reversed
order, swap them.
◦ If at least one swap has been done, repeat step 1.

11/12/2024 6
Bubble Sort

11/12/2024 7
Bubble Sort
Begin
for k = 1 to (n-1) by 1 do
for j = 0 to (n-k-1) by 1 do
If ( a[j] > a[j+1] ) then
temp = a [j]
a[j] = a
[j+1]
a[j+1]= temp
End if
End for
End for
End
Analysis

F(n) = (n-1) +(n-2) +………+/3


+2+1
= n (n-1 ) / 2
= O (n2 ) 11/12/2024 8
Insertion Sort
 Insertion sort belongs to the O(n2) sorting algorithms.
 Unlike many sorting algorithms with quadratic
complexity, it is actually applied in practice for sorting
small arrays of data.

Algorithm
 Insertion sort algorithm 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.

11/12/2024 9
Insertion Sort

Example: Sort {7, -5, 2, 16, 4} using insertion sort.

11/12/2024 10
Insertion Sort

11/12/2024 11
Analysis [no of comparisons ]

F(n) = 1 + 2 + 3 + ………(n-3) +(n-2)+ (n-


1)
= n (n-1 ) / 2
Insertion Sort (A) = O (n2 )

for j=2 to A.length


key = A[j]
//insert A[j] into sorted sequence A[1..j-1]
i=j-1
while (i>0 and A[i] > key )
A[i+1] =A [i]
i=i-1
A[i+1]= key
End for

11/12/2024 12

You might also like