KEMBAR78
Lecture 1 sorting insertion & shell sort | PPTX
Insertion Sort & Shell Sort
Sorting
Is a process through which the data are arranged according
to their values.
Why sorting?
Sorting Algorithms
Insertion sort
Shell sort
Heap sort
Quick sort
Merge sort
Bucket sort
Radix sort
Insertion sort
The list is divided in to 2 parts: sorted & unsorted
In each pass the 1st element of the unsorted sub-list is
transferred to the sorted sub-list by inserting it at the
appropriate place.
If the list contains ‘n’ elements then there will be ‘n- 1’
passes.
Example
Algorithm
Insertion sort:
Step 1: Repeat Step 2 to 5 for k=1 to N
Setp 2: Set Temp=arr[k]
Setp 3: Set j=k
Setp 4: Repeat while j>0 && temp<=arr[j-1]
Step 4.1: Set arr[j]=arr[j-1]
step 4.2: Set j=j-1
Step 5:Set arr[j]=Temp
Step 6: Exit
Shell Sort
Created by Donald L. Shell
Improved version of the insertion sort
In this a list of N element is divided into K segments,
where K is known as the increment.
Each pass through the data starts with the 1st element
in the array and progresses through the array,
comparing adjacent elements in each segment.
Example
Example
Example
Algorithm
shellsort( input_type a[ ], unsigned int n )
{
unsigned int i, j, increment;
input_type tmp;
1. for( increment = n/2; increment > 0; increment /= 2 )
2. for( i = increment; i<n; i++ )
{
3. tmp = a[i];
4. for( j = i; j >= increment; j -= increment )
5. if( tmp < a[j-increment] )
6. a[j] = a[j-increment];
Else
7. break;
8. a[j] = tmp;
}
}
Exercise
1. Write a C-program to implement Insertion Sort.
2. Write a C-program to implement Shell Sort.
3. Perform Insertion sort with the help of the algorithm and
show 2-passes for the following sequence.
20,40,30,10,60
4. Perform Shell sort with the help of the algorithm and show
2-Increments for the following sequence.
30, 40, 20, 15, 25, 10, 35, 4
Reference Books
1. Data Structures & Algorithm Analysis in C
Mark Allen Weiss
2. Data Structures –A Pseudocode Approach with C
Gilberg & Forouzan

Lecture 1 sorting insertion &amp; shell sort

  • 1.
    Insertion Sort &Shell Sort
  • 2.
    Sorting Is a processthrough which the data are arranged according to their values. Why sorting?
  • 3.
    Sorting Algorithms Insertion sort Shellsort Heap sort Quick sort Merge sort Bucket sort Radix sort
  • 4.
    Insertion sort The listis divided in to 2 parts: sorted & unsorted In each pass the 1st element of the unsorted sub-list is transferred to the sorted sub-list by inserting it at the appropriate place. If the list contains ‘n’ elements then there will be ‘n- 1’ passes.
  • 5.
  • 6.
    Algorithm Insertion sort: Step 1:Repeat Step 2 to 5 for k=1 to N Setp 2: Set Temp=arr[k] Setp 3: Set j=k Setp 4: Repeat while j>0 && temp<=arr[j-1] Step 4.1: Set arr[j]=arr[j-1] step 4.2: Set j=j-1 Step 5:Set arr[j]=Temp Step 6: Exit
  • 7.
    Shell Sort Created byDonald L. Shell Improved version of the insertion sort In this a list of N element is divided into K segments, where K is known as the increment. Each pass through the data starts with the 1st element in the array and progresses through the array, comparing adjacent elements in each segment.
  • 8.
  • 9.
  • 10.
  • 11.
    Algorithm shellsort( input_type a[], unsigned int n ) { unsigned int i, j, increment; input_type tmp; 1. for( increment = n/2; increment > 0; increment /= 2 ) 2. for( i = increment; i<n; i++ ) { 3. tmp = a[i]; 4. for( j = i; j >= increment; j -= increment ) 5. if( tmp < a[j-increment] ) 6. a[j] = a[j-increment]; Else 7. break; 8. a[j] = tmp; } }
  • 12.
    Exercise 1. Write aC-program to implement Insertion Sort. 2. Write a C-program to implement Shell Sort. 3. Perform Insertion sort with the help of the algorithm and show 2-passes for the following sequence. 20,40,30,10,60 4. Perform Shell sort with the help of the algorithm and show 2-Increments for the following sequence. 30, 40, 20, 15, 25, 10, 35, 4
  • 13.
    Reference Books 1. DataStructures & Algorithm Analysis in C Mark Allen Weiss 2. Data Structures –A Pseudocode Approach with C Gilberg & Forouzan