SlideShare ist ein Scribd-Unternehmen logo
1 von 20
19CB203-DATA STRUCTURES AND
ALGORITHMS
PREPARED AND PRESENTED BY
SUPRIYA U, AP/CSBS
TOPICS
•INTRODUCTION
•INSERTION SORT
•SELECTION SORT
•SHELL SORT
INTRODUCTION
Sorting is the process of arranging the elements of an array so that they can be placed either
in ascending or descending order. For example, consider an array A = {A1, A2, A3, A4, ?? An },
the array is called to be in ascending order if element of A are arranged like A1 > A2 > A3 >
A4 > A5 > ? > An .
Consider an array;
int A[10] = { 5, 4, 10, 2, 30, 45, 34, 14, 18, 9 )
The Array sorted in ascending order will be given as;
A[] = { 2, 4, 5, 9, 10, 14, 18, 30, 34, 45 }
INSERTION SORT
Insertion sort is the simple sorting algorithm which is commonly used in
the daily lives while ordering a deck of cards.
In this algorithm, we insert each element onto its proper place in the
sorted array.
This is less efficient than the other sort algorithms like quick sort, merge
sort, etc.
INSERTION SORT TECHNIQUE
Consider an array A whose elements are to be sorted. Initially, A[0] is the only
element on the sorted set. In pass 1, A[1] is placed at its proper index in the
array.
In pass 2, A[2] is placed at its proper index in the array. Likewise, in pass n-1,
A[n-1] is placed at its proper index into the array.
To insert an element A[k] to its proper index, we must compare it with all other
elements i.e. A[k-1], A[k-2], and so on until we find an element A[j] such that,
A[j]<=A[k].
All the elements from A[k-1] to A[j] need to be shifted and A[k] will be moved to
A[j+1].
INSERTION SORT -COMPLEXITY
INSERTION SORT -ALGORITHM
Step 1: Repeat Steps 2 to 5 for K = 1 to N-1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <=ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
INSERTION SORT-EXAMPLE
#include<stdio.h>
void main ()
{
int i,j, k,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
printf("n printing sorted elements...n");
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1; }
a[j+1] = temp; }
for(i=0;i<10;i++)
{
printf("n%dn",a[i]);
}
}
SELECTION SORT
• In selection sort, the smallest value among the unsorted elements of the array is
selected in every pass and inserted to its appropriate position into the array.
• First, find the smallest element of the array and place it on the first position. Then,
find the second smallest element of the array and place it on the second position.
The process continues until we get the sorted array.
• The array with n elements is sorted by using n-1 pass of selection sort algorithm.
• In 1st pass, smallest element of the array is to be found along with its index pos.
then, swap A[0] and A[pos]. Thus A[0] is sorted, we now have n -1 elements which
are to be sorted.
• In 2nd pas, position pos of the smallest element present in the sub-array A[n-1] is
found. Then, swap, A[1] and A[pos]. Thus A[0] and A[1] are sorted, we now left with
n-2 unsorted elements.
• In n-1th pass, position pos of the smaller element between A[n-1] and A[n-2] is to be
found. Then, swap, A[pos] and A[n-1].
SELECTION SORT-EXAMPLE
Consider the following array with 6 elements. Sort the elements of the
array by using selection sort.
A = {10, 2, 3, 90, 43, 56}.
SELECTION SORT -COMPLEXITY
SELECTION SORT -ALGORITHM
SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT
SMALLEST (ARR, K, N, POS)
Step 1: [INITIALIZE] SETSMALL = ARR[K]
Step 2: [INITIALIZE] SETPOS = K
Step 3: Repeat for J = K+1 to N -1
IFSMALL > ARR[J]
SETSMALL = ARR[J]
SETPOS = J
[END OF IF]
[END OF LOOP]
Step 4: RETURN POS
SELECTION SORT-EXAMPLE
#include<stdio.h>
int smallest(int[],int,int);
void main ()
{
int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i,j,k,pos,temp;
for(i=0;i<10;i++)
{
pos = smallest(a,10,i);
temp = a[i];
a[i]=a[pos];
a[pos] = temp;
}
printf("nprinting sorted elements...n");
for(i=0;i<10;i++)
{
printf("%dn",a[i]);
}
}
int smallest(int a[], int n, int i)
{
int small,pos,j;
small = a[i];
pos = i;
for(j=i+1;j<10;j++)
{
if(a[j]<small)
{
small = a[j];
pos=j;
}
}
return pos;
}
SELECTION SORT-EXAMPLE
SHELL SORT
• Shell sort is the generalization of insertion sort which overcomes the drawbacks of insertion
sort by comparing elements separated by a gap of several positions. In general, Shell sort
performs the following steps.
• Step 1: Arrange the elements in the tabular form and sort the columns by using insertion sort.
• Step 2: Repeat Step 1; each time with smaller number of longer columns in such a way that at
the end, there is only one column of data to be sorted.
SHELL SORT
SHELL SORT -ALGORITHM
SHELL SORT-EXAMPLE
#include <stdio.h>
void shellsort(int arr[], int num)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
} }
int main()
{
int arr[30];
int k, num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
printf("nEnter %d numbers: ", num);
for (k = 0 ; k < num; k++)
{
scanf("%d", &arr[k]);
}
shellsort(arr, num);
printf("n Sorted array is: ");
for (k = 0; k < num; k++)
printf("%d ", arr[k]);
return 0;
}
SHELL SORT-EXAMPLE
sorting1.pptx

Weitere ähnliche Inhalte

Ähnlich wie sorting1.pptx

Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
Deepak Singh
 

Ähnlich wie sorting1.pptx (20)

358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
1D Array
1D Array1D Array
1D Array
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Array data structure
Array data structureArray data structure
Array data structure
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
 
Queue
QueueQueue
Queue
 
Array data structure
Array data structureArray data structure
Array data structure
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
Analysis of algorithms
Analysis of algorithms Analysis of algorithms
Analysis of algorithms
 
Lecture 1 sorting insertion &amp; shell sort
Lecture 1 sorting insertion &amp; shell sortLecture 1 sorting insertion &amp; shell sort
Lecture 1 sorting insertion &amp; shell sort
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 

Mehr von AJAYVISHALRP (10)

finalppt-150606051347-lva1-app6892.pptx
finalppt-150606051347-lva1-app6892.pptxfinalppt-150606051347-lva1-app6892.pptx
finalppt-150606051347-lva1-app6892.pptx
 
WH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptxWH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptx
 
Data Storage Access and Security.pptx
Data Storage Access and Security.pptxData Storage Access and Security.pptx
Data Storage Access and Security.pptx
 
U3-PPT-1 (1).ppt
U3-PPT-1 (1).pptU3-PPT-1 (1).ppt
U3-PPT-1 (1).ppt
 
U2-LP2.ppt
U2-LP2.pptU2-LP2.ppt
U2-LP2.ppt
 
U1-LP1.ppt
U1-LP1.pptU1-LP1.ppt
U1-LP1.ppt
 
disk scheduling algorithms.pptx
disk scheduling algorithms.pptxdisk scheduling algorithms.pptx
disk scheduling algorithms.pptx
 
1G1.pptx
1G1.pptx1G1.pptx
1G1.pptx
 
WH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptxWH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptx
 
AI Pugal.pptx
AI Pugal.pptxAI Pugal.pptx
AI Pugal.pptx
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

sorting1.pptx

  • 1. 19CB203-DATA STRUCTURES AND ALGORITHMS PREPARED AND PRESENTED BY SUPRIYA U, AP/CSBS
  • 3. INTRODUCTION Sorting is the process of arranging the elements of an array so that they can be placed either in ascending or descending order. For example, consider an array A = {A1, A2, A3, A4, ?? An }, the array is called to be in ascending order if element of A are arranged like A1 > A2 > A3 > A4 > A5 > ? > An . Consider an array; int A[10] = { 5, 4, 10, 2, 30, 45, 34, 14, 18, 9 ) The Array sorted in ascending order will be given as; A[] = { 2, 4, 5, 9, 10, 14, 18, 30, 34, 45 }
  • 4. INSERTION SORT Insertion sort is the simple sorting algorithm which is commonly used in the daily lives while ordering a deck of cards. In this algorithm, we insert each element onto its proper place in the sorted array. This is less efficient than the other sort algorithms like quick sort, merge sort, etc.
  • 5. INSERTION SORT TECHNIQUE Consider an array A whose elements are to be sorted. Initially, A[0] is the only element on the sorted set. In pass 1, A[1] is placed at its proper index in the array. In pass 2, A[2] is placed at its proper index in the array. Likewise, in pass n-1, A[n-1] is placed at its proper index into the array. To insert an element A[k] to its proper index, we must compare it with all other elements i.e. A[k-1], A[k-2], and so on until we find an element A[j] such that, A[j]<=A[k]. All the elements from A[k-1] to A[j] need to be shifted and A[k] will be moved to A[j+1].
  • 7. INSERTION SORT -ALGORITHM Step 1: Repeat Steps 2 to 5 for K = 1 to N-1 Step 2: SET TEMP = ARR[K] Step 3: SET J = K - 1 Step 4: Repeat while TEMP <=ARR[J] SET ARR[J + 1] = ARR[J] SET J = J - 1 [END OF INNER LOOP] Step 5: SET ARR[J + 1] = TEMP [END OF LOOP] Step 6: EXIT
  • 8. INSERTION SORT-EXAMPLE #include<stdio.h> void main () { int i,j, k,temp; int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; printf("n printing sorted elements...n"); for(k=1; k<10; k++) { temp = a[k]; j= k-1; while(j>=0 && temp <= a[j]) { a[j+1] = a[j]; j = j-1; } a[j+1] = temp; } for(i=0;i<10;i++) { printf("n%dn",a[i]); } }
  • 9. SELECTION SORT • In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array. • First, find the smallest element of the array and place it on the first position. Then, find the second smallest element of the array and place it on the second position. The process continues until we get the sorted array. • The array with n elements is sorted by using n-1 pass of selection sort algorithm. • In 1st pass, smallest element of the array is to be found along with its index pos. then, swap A[0] and A[pos]. Thus A[0] is sorted, we now have n -1 elements which are to be sorted. • In 2nd pas, position pos of the smallest element present in the sub-array A[n-1] is found. Then, swap, A[1] and A[pos]. Thus A[0] and A[1] are sorted, we now left with n-2 unsorted elements. • In n-1th pass, position pos of the smaller element between A[n-1] and A[n-2] is to be found. Then, swap, A[pos] and A[n-1].
  • 10. SELECTION SORT-EXAMPLE Consider the following array with 6 elements. Sort the elements of the array by using selection sort. A = {10, 2, 3, 90, 43, 56}.
  • 12. SELECTION SORT -ALGORITHM SELECTION SORT(ARR, N) Step 1: Repeat Steps 2 and 3 for K = 1 to N-1 Step 2: CALL SMALLEST(ARR, K, N, POS) Step 3: SWAP A[K] with ARR[POS] [END OF LOOP] Step 4: EXIT SMALLEST (ARR, K, N, POS) Step 1: [INITIALIZE] SETSMALL = ARR[K] Step 2: [INITIALIZE] SETPOS = K Step 3: Repeat for J = K+1 to N -1 IFSMALL > ARR[J] SETSMALL = ARR[J] SETPOS = J [END OF IF] [END OF LOOP] Step 4: RETURN POS
  • 13. SELECTION SORT-EXAMPLE #include<stdio.h> int smallest(int[],int,int); void main () { int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; int i,j,k,pos,temp; for(i=0;i<10;i++) { pos = smallest(a,10,i); temp = a[i]; a[i]=a[pos]; a[pos] = temp; } printf("nprinting sorted elements...n"); for(i=0;i<10;i++) { printf("%dn",a[i]); } } int smallest(int a[], int n, int i) { int small,pos,j; small = a[i]; pos = i; for(j=i+1;j<10;j++) { if(a[j]<small) { small = a[j]; pos=j; } } return pos; }
  • 15. SHELL SORT • Shell sort is the generalization of insertion sort which overcomes the drawbacks of insertion sort by comparing elements separated by a gap of several positions. In general, Shell sort performs the following steps. • Step 1: Arrange the elements in the tabular form and sort the columns by using insertion sort. • Step 2: Repeat Step 1; each time with smaller number of longer columns in such a way that at the end, there is only one column of data to be sorted.
  • 18. SHELL SORT-EXAMPLE #include <stdio.h> void shellsort(int arr[], int num) { int i, j, k, tmp; for (i = num / 2; i > 0; i = i / 2) { for (j = i; j < num; j++) { for(k = j - i; k >= 0; k = k - i) { if (arr[k+i] >= arr[k]) break; else { tmp = arr[k]; arr[k] = arr[k+i]; arr[k+i] = tmp; } } } } } int main() { int arr[30]; int k, num; printf("Enter total no. of elements : "); scanf("%d", &num); printf("nEnter %d numbers: ", num); for (k = 0 ; k < num; k++) { scanf("%d", &arr[k]); } shellsort(arr, num); printf("n Sorted array is: "); for (k = 0; k < num; k++) printf("%d ", arr[k]); return 0; }