KEMBAR78
Different Sorting tecniques in Data Structure | PPTX
BIRLA VISHVAKARMA
MAHAVIDHYALAYA
Sorting in DATA STRUCTURE
Sr. No. Names Enrollment No.
1. Rudra Patel 140080116050
2. Rishab Shah 140080116054
3. Tushar Gonawala 140080116059
4. Rushang Patel 140080116064
PRESENTED TO: Prof. Vikram
SORTING
• Sorting is the process of ordering a list of elements in either ascending order or descending order.
• Sorting can be divided into two categories:
1. Internal Sorting: takes place in the main memory of the computer. It can take advantage of
the random access nature of the main memory. Element to be sorted is stored in an
integer array.
2. External Sorting: is carried on secondary memory. It becomes a necessity if the number of
elements to be sorted is too large to be fit in the main memory. Algorithm should always
take into account that movement of data between secondary storage and main storage is
best done by moving a block of contiguous element.
INSERTION SORT
• Insertion sort is based on the principle of inserting the element at its correct place in a previously
sorted list. It can be varied from 1 to n-1 to sort the entire array.
1st Iteration
0 5 1 9 2 6 4
2nd Iteration
0 1 5 9 2 6 4
3rd Iteration
0 1 5 9 2 6 4
4th Iteration
0 1 2 5 9 6 4
5th Iteration
0 1 2 5 6 9 4
6th Iteration
0 1 2 4 5 9 6
Sorted Unsorted
Sorted
Sorted
Sorted
Sorted
Sorted
Unsorted
Unsorted
Unsorted
Unsorted
ALGORITHM
void insertion_sort( int a[ ] , int n)
{
int I, j, temp;
for (i = 1 ; i < n ; i++)
{
temp = a[i];
for (j = i - 1 ; j >= 0 && a[j] > temp ; j--)
a[j + 1] = a[j];
a[j +1] = temp;
}
}
BUBBLE SORT
• Bubble sort is one of the simplest and the most popular sorting method. The basic idea behind
bubble sort is as a bubble rises up in water, the smallest element goes to the beginning. This
method is based on the successive selecting the smallest element through exchange of adjacent
element.
j = 0 5 6 2 8 1
j = 1 5 6 2 8 1
j = 2 5 2 6 8 1
j = 3 5 2 6 8 1
j = 0 5 2 6 1 8
j = 1 2 5 6 1 8
j = 2 2 5 6 1 8
j = 0 2 5 1 6 8
j = 1 2 5 1 6 8
j = 0 2 1 5 6 8
Sorted Array: 1 2 5 6 8
First Pass
i = 1
Second Pass
i = 2
Third Pass
i = 3
Fourth Pass
i = 1
ALGORITHM
void bubble_sort( int a[ ] , int n)
{
int I, j, temp;
for (i = 1 ; i < n ; i++)
for (j = i - 1 ; j <= n - i ; j++)
if(a[j] > a[j + 1])
{
temp = a[i];
a[j + 1] = a[j];
a[j] = temp;
}
}
SELECTION SORT
• Selection sort is a very simple sorting method. In the ith pass, we select the element with the
lowest value amongst a[i], a[i + 1],…,a[n – 1] and we swap it with a[i]. As a result, after i passes
first element will be in sorted order.
5 9 1 11 2 4 Original Array
1 9 5 11 2 4 After First Pass
1 2 5 11 9 4 After Second Pass
1 2 4 11 9 5 After Third Pass
1 2 4 5 9 11 After Fourth Pass
1 2 4 5 9 11 After Fifth Pass
ALGORITHM
void _sort( int a[ ] , int n)
{
int i, j, temp;
for (i = 0 ; i < n - 1 ; i++)
{
k = i;
for (j = i + 1 ; j < n ; j++)
if(a[j] < a[k])
k = j;
if ( k != i )
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
Quick Sort
• Quick sort is the fastest internal sorting algorithm.
5 1 2 9 0 8 13
5 1 2 9 0 8 13
1 2 0
1 2 0
9 8 13
9 8 13
5
0
1
2 8
9
13
0 1 2 5 8 9 13
ALGORITHM
void _sort( int a[ ] , int n)
{
int i, j, temp;
for (i = 0 ; i < n - 1 ; i++)
{
k = i;
for (j = i + 1 ; j < n ; j++)
if(a[j] < a[k])
k = j;
if ( k != i )
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}

Different Sorting tecniques in Data Structure

  • 1.
    BIRLA VISHVAKARMA MAHAVIDHYALAYA Sorting inDATA STRUCTURE Sr. No. Names Enrollment No. 1. Rudra Patel 140080116050 2. Rishab Shah 140080116054 3. Tushar Gonawala 140080116059 4. Rushang Patel 140080116064 PRESENTED TO: Prof. Vikram
  • 2.
    SORTING • Sorting isthe process of ordering a list of elements in either ascending order or descending order. • Sorting can be divided into two categories: 1. Internal Sorting: takes place in the main memory of the computer. It can take advantage of the random access nature of the main memory. Element to be sorted is stored in an integer array. 2. External Sorting: is carried on secondary memory. It becomes a necessity if the number of elements to be sorted is too large to be fit in the main memory. Algorithm should always take into account that movement of data between secondary storage and main storage is best done by moving a block of contiguous element.
  • 3.
    INSERTION SORT • Insertionsort is based on the principle of inserting the element at its correct place in a previously sorted list. It can be varied from 1 to n-1 to sort the entire array. 1st Iteration 0 5 1 9 2 6 4 2nd Iteration 0 1 5 9 2 6 4 3rd Iteration 0 1 5 9 2 6 4 4th Iteration 0 1 2 5 9 6 4 5th Iteration 0 1 2 5 6 9 4 6th Iteration 0 1 2 4 5 9 6 Sorted Unsorted Sorted Sorted Sorted Sorted Sorted Unsorted Unsorted Unsorted Unsorted
  • 4.
    ALGORITHM void insertion_sort( inta[ ] , int n) { int I, j, temp; for (i = 1 ; i < n ; i++) { temp = a[i]; for (j = i - 1 ; j >= 0 && a[j] > temp ; j--) a[j + 1] = a[j]; a[j +1] = temp; } }
  • 5.
    BUBBLE SORT • Bubblesort is one of the simplest and the most popular sorting method. The basic idea behind bubble sort is as a bubble rises up in water, the smallest element goes to the beginning. This method is based on the successive selecting the smallest element through exchange of adjacent element. j = 0 5 6 2 8 1 j = 1 5 6 2 8 1 j = 2 5 2 6 8 1 j = 3 5 2 6 8 1 j = 0 5 2 6 1 8 j = 1 2 5 6 1 8 j = 2 2 5 6 1 8 j = 0 2 5 1 6 8 j = 1 2 5 1 6 8 j = 0 2 1 5 6 8 Sorted Array: 1 2 5 6 8 First Pass i = 1 Second Pass i = 2 Third Pass i = 3 Fourth Pass i = 1
  • 6.
    ALGORITHM void bubble_sort( inta[ ] , int n) { int I, j, temp; for (i = 1 ; i < n ; i++) for (j = i - 1 ; j <= n - i ; j++) if(a[j] > a[j + 1]) { temp = a[i]; a[j + 1] = a[j]; a[j] = temp; } }
  • 7.
    SELECTION SORT • Selectionsort is a very simple sorting method. In the ith pass, we select the element with the lowest value amongst a[i], a[i + 1],…,a[n – 1] and we swap it with a[i]. As a result, after i passes first element will be in sorted order. 5 9 1 11 2 4 Original Array 1 9 5 11 2 4 After First Pass 1 2 5 11 9 4 After Second Pass 1 2 4 11 9 5 After Third Pass 1 2 4 5 9 11 After Fourth Pass 1 2 4 5 9 11 After Fifth Pass
  • 8.
    ALGORITHM void _sort( inta[ ] , int n) { int i, j, temp; for (i = 0 ; i < n - 1 ; i++) { k = i; for (j = i + 1 ; j < n ; j++) if(a[j] < a[k]) k = j; if ( k != i ) { temp = a[i]; a[i] = a[k]; a[k] = temp; } } }
  • 9.
    Quick Sort • Quicksort is the fastest internal sorting algorithm. 5 1 2 9 0 8 13 5 1 2 9 0 8 13 1 2 0 1 2 0 9 8 13 9 8 13 5 0 1 2 8 9 13 0 1 2 5 8 9 13
  • 10.
    ALGORITHM void _sort( inta[ ] , int n) { int i, j, temp; for (i = 0 ; i < n - 1 ; i++) { k = i; for (j = i + 1 ; j < n ; j++) if(a[j] < a[k]) k = j; if ( k != i ) { temp = a[i]; a[i] = a[k]; a[k] = temp; } } }