BCAC 232: Data Structures Insertion sort This

Published  . 0 views
↓ Download
BCAC 232: Data Structures Insertion sort This
1 / 1
BCAC 232: Data Structures Insertion sort This - slide 1 of 9 BCAC 232: Data Structures Insertion sort This - slide 2 of 9 BCAC 232: Data Structures Insertion sort This - slide 3 of 9 BCAC 232: Data Structures Insertion sort This - slide 4 of 9 BCAC 232: Data Structures Insertion sort This - slide 5 of 9 BCAC 232: Data Structures Insertion sort This - slide 6 of 9 BCAC 232: Data Structures Insertion sort This - slide 7 of 9 BCAC 232: Data Structures Insertion sort This - slide 8 of 9 BCAC 232: Data Structures Insertion sort This - slide 9 of 9
Description: BCAC 232: Data Structures Insertion sort This sorts a set of records by inserting records into an existing sorted file. Working of an Insertion Sort A0 A1 A2 A3 A4 A5 A Insertion sort compares the first two elements And finds

Related Topics

Download Presentation

"BCAC 232: Data Structures Insertion sort This" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide1. BCAC 232: Data Structures<br>
slide2. Insertion sort This sorts a set of records by inserting records into an existing sorted file.
Working of an Insertion Sort
A[0] A[1] A[2] A[3] A[4] A[5]
A
Insertion sort compares the first two elements

And finds that 50 is not in the correct position. It swaps 50 with 25.

Insertion sort moves ahead and compares 50 with 10.

It swaps 50 with 10. It also checks with all the elements of sorted sub-list. Here we see that sorted sub-list has only one element 25, and 10 is smaller than 25. Hence, It swaps 25 with 10.<br>
slide3. In this step it compares 60 with 45.

It swaps 60 with 45. It also checks with all the elements of sorted sub-list. Here sorted sub-list have element 10,25 and 50. and 50 is greater than 45. It swaps 50 with 45.
and then next compares 45 with 25. Hence, 45 is greater than 25, no need to swap numbers. It remains same.

In this steps it compares 60 with 33.

It swaps 60 with 33. It also checks with all the elements of sorted sub-list. This process goes on until all the unsorted values are covered in a sorted sub-list.

Final Sorted list is<br>
slide4. Insertion(x[],n)
{
int i,j,key;
for(i = 1;i < n ; i++)
{
key = x[i];
for(j = i – 1; j >= 0 && key < x[j] ; j--)
{
x[j+1] = x[j];
}
x[j+1] = key;
}
}<br>
slide5. Radix sort One might want to sort numbers on their most significant digit. However, Radix sort works counter-intuitively by sorting on the least significant digits first.

On the first pass, all the numbers are sorted on the least significant digit and combined in an array.

Then on the second pass, the entire numbers are sorted again on the second least significant digits and combined in an array and so on.<br>
slide6. Consider an Array for Example
0 1 2 3 4 5 6 7

Least significant digit
Q[0]  20 60 10
Q[1] 
Q[2] 
Q[3] 
Q[4] 
Q[5]  45 75 55 35
Q[6] 
Q[7]  27
Q[8] 
Q[9] <br>
slide7. Most Significant digit

Q[0] 
Q[1]  10
Q[2]  20 27
Q[3]  35
Q[4]  45
Q[5]  55
Q[6]  60
Q[7]  75
Q[8] 
Q[9] 
Final sorted list is<br>
slide8. Algorithm for(k = least significant digit; k <= most significant digit; k++)
{
for(i=0;i<n;i++)
{
Y = X[i];
j = kth digit of y
place y at rear end of queue[j]
}
for(queue=0;queue<10;queue++)
{
place element of queue[queue] in the next sequential position of x
}
}<br>
slide9. THANK YOU<br>