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

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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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...Neo4j
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
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
 
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
 
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...Martijn de Jong
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
🐬 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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
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
 

Dernier (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
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...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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
 

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