SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à sorting1.pptx

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-14sumitbardhan
 
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 sortShanmuganathan C
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Ra'Fat Al-Msie'deen
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
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 2024RUHULAMINHAZARIKA
 
Array data structure
Array data structureArray data structure
Array data structureharsh112327
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimensionDeepak Singh
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
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 .pptxabhishekmaurya102515
 
Analysis of algorithms
Analysis of algorithms Analysis of algorithms
Analysis of algorithms MUSAIDRIS15
 
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 sortAbirami A
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data StructureMandeep Singh
 

Similaire à 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
 

Plus de AJAYVISHALRP

finalppt-150606051347-lva1-app6892.pptx
finalppt-150606051347-lva1-app6892.pptxfinalppt-150606051347-lva1-app6892.pptx
finalppt-150606051347-lva1-app6892.pptxAJAYVISHALRP
 
WH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptxWH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptxAJAYVISHALRP
 
Data Storage Access and Security.pptx
Data Storage Access and Security.pptxData Storage Access and Security.pptx
Data Storage Access and Security.pptxAJAYVISHALRP
 
disk scheduling algorithms.pptx
disk scheduling algorithms.pptxdisk scheduling algorithms.pptx
disk scheduling algorithms.pptxAJAYVISHALRP
 
WH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptxWH PAPER PRESENTATION PPT.pptx
WH PAPER PRESENTATION PPT.pptxAJAYVISHALRP
 

Plus de 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
 

Dernier

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.pdfhans926745
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
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 FresherRemote DBA Services
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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?Antenna Manufacturer Coco
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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 organizationRadu Cotescu
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Dernier (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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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?
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

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; }