SlideShare a Scribd company logo
SORTING ALGORITHMS
Engr. Shaheer Ahmed
ALGO - INSERTION SORT (N, A)
A : Array
N : Length of Array A[]
Start Algo
For i:=1 to N-1 do
value := A[i]
j := i-1
while j >= 0 and A[j] > value do
A[j+1] := A[j]
j := j-1
endwhile
A[j+1] = value
Endfor
End Algo
Program – Insertion Sort
#include<iostream>
using namespace std;
main()
{
int A[6] = {6,5,4,3,2,1};
int N=6;
int i,j,val;
for(i=1;i<N;i++)
{
val=A[i];
j=i-1;
while(j>=0 && A[j]>val)
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=val;
}
cout<<endl;
for(int i = 0 ; i<N ; i++)
cout<<A[i]<<" ";
system("pause");
}
Insertion Sort
Worst case analysis of Insertion Sort
Apply Insertion Sort on the following Data:
7,6,5,4,3,2,1
Selection Sort
Program – Selection Sort
#include<iostream>
using namespace std;
main()
{
int A[6] = {5,2,6,4,3,1};
int N=6;
int i,sel=0,min,index,swap=0, temp;
//Find smallest number and swap with selected index with each iteration of outer loop
while(sel<N)
{
min = A[sel];
for(i = sel; i< N; i++)
{
if(A[i]<min) //if smaller number is found
{
min=A[i];
index=i; // save index of smaller number
swap=1; // swapping required..
}
}
//swapping
if(swap==1)
{
A[sel] = A[sel] + A[index]; // a = a+b
A[index] = A[sel] - A[index]; // b = a - b
A[sel] = A[sel] - A[index]; // a = a-b
}
sel++;
swap=0;
}
for(int i = 0 ; i<N ; i++)
cout<<A[i]<<" ";
system("pause");
}
Apply Selection Sort on the following Data:
7,6,5,4,3,2,1
Worst case analysis of Selection Sort
Quick Sort
15 20 35 10 25 55 50 45 40 30
3015 20 10 25 35 55 50 45 40
3015 20 2510 4535 40 50 55
15 2010 25 35 4540 50 55
10 15 20 25 30 35 40 45 50 55
30
MERGE SORT
15 , 20 , 35 , 10 , 25 , 55 , 50 , 45
15 20 10 35 25 55 45 50
10 15 20 35 25 45 50 55
10 15 20 25 35 45 50 55
GIVEN DATA
Ad

More Related Content

Viewers also liked (20)

04 sorting
04 sorting04 sorting
04 sorting
martchasera92
 
CHC Finance: Using the New IRS 990 Form
CHC Finance: Using the New IRS 990 FormCHC Finance: Using the New IRS 990 Form
CHC Finance: Using the New IRS 990 Form
Phillip Bergquist - MPCA
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
maamir farooq
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Dorina Isaj
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
grahamwell
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
Binary Search Algorithm
Binary Search Algorithm Binary Search Algorithm
Binary Search Algorithm
Anastasia Jakubow
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
A project report on chat application
A project report on chat applicationA project report on chat application
A project report on chat application
Kumar Gaurav
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
MG Thushara Pradeesh
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Trupti Agrawal
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Vicente García Díaz
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
maamir farooq
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
grahamwell
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
A project report on chat application
A project report on chat applicationA project report on chat application
A project report on chat application
Kumar Gaurav
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 

Similar to Sorting Technique (20)

C programs
C programsC programs
C programs
Lakshmi Sarvani Videla
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
Ramachendran Logarajah
 
Performance Portability Through Descriptive Parallelism
Performance Portability Through Descriptive ParallelismPerformance Portability Through Descriptive Parallelism
Performance Portability Through Descriptive Parallelism
Jeff Larkin
 
check the modifed code now you will get all operations done.termin.pdf
check the modifed code now you will get all operations done.termin.pdfcheck the modifed code now you will get all operations done.termin.pdf
check the modifed code now you will get all operations done.termin.pdf
angelfragranc
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
Aaex2 group2
Aaex2 group2Aaex2 group2
Aaex2 group2
Shiang-Yun Yang
 
#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdf#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdf
ANSAPPARELS
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
shreeaadithyaacellso
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
Chap06alg
Chap06algChap06alg
Chap06alg
Munkhchimeg
 
Chap06alg
Chap06algChap06alg
Chap06alg
Munhchimeg
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
Kandarp Tiwari
 
insertion sort.pptx
insertion sort.pptxinsertion sort.pptx
insertion sort.pptx
TejaswiniMohalkar
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdf
aroramobiles1
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
AJAYVISHALRP
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
Performance Portability Through Descriptive Parallelism
Performance Portability Through Descriptive ParallelismPerformance Portability Through Descriptive Parallelism
Performance Portability Through Descriptive Parallelism
Jeff Larkin
 
check the modifed code now you will get all operations done.termin.pdf
check the modifed code now you will get all operations done.termin.pdfcheck the modifed code now you will get all operations done.termin.pdf
check the modifed code now you will get all operations done.termin.pdf
angelfragranc
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdf#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdf
ANSAPPARELS
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
shreeaadithyaacellso
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
Kandarp Tiwari
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdf
aroramobiles1
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
Ad

More from Salman Vadsarya (7)

Mobile communications
Mobile communicationsMobile communications
Mobile communications
Salman Vadsarya
 
Expression trees
Expression treesExpression trees
Expression trees
Salman Vadsarya
 
The job application process interview
The job application process interviewThe job application process interview
The job application process interview
Salman Vadsarya
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
Salman Vadsarya
 
Arificail Intelligence and robotics
Arificail Intelligence and roboticsArificail Intelligence and robotics
Arificail Intelligence and robotics
Salman Vadsarya
 
History of computer
History of computerHistory of computer
History of computer
Salman Vadsarya
 
The job application process interview
The job application process interviewThe job application process interview
The job application process interview
Salman Vadsarya
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
Salman Vadsarya
 
Arificail Intelligence and robotics
Arificail Intelligence and roboticsArificail Intelligence and robotics
Arificail Intelligence and robotics
Salman Vadsarya
 
Ad

Recently uploaded (20)

How to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdfHow to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdf
victordsane
 
Navigating EAA Compliance in Testing.pdf
Navigating EAA Compliance in Testing.pdfNavigating EAA Compliance in Testing.pdf
Navigating EAA Compliance in Testing.pdf
Applitools
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Implementing promises with typescripts, step by step
Implementing promises with typescripts, step by stepImplementing promises with typescripts, step by step
Implementing promises with typescripts, step by step
Ran Wahle
 
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEMGDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
philipnathen82
 
Best Practices for Collaborating with 3D Artists in Mobile Game Development
Best Practices for Collaborating with 3D Artists in Mobile Game DevelopmentBest Practices for Collaborating with 3D Artists in Mobile Game Development
Best Practices for Collaborating with 3D Artists in Mobile Game Development
Juego Studios
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
saimabibi60507
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Microsoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptxMicrosoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptx
Mekonnen
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Streamline Your Manufacturing Data. Strengthen Every Operation.
Streamline Your Manufacturing Data. Strengthen Every Operation.Streamline Your Manufacturing Data. Strengthen Every Operation.
Streamline Your Manufacturing Data. Strengthen Every Operation.
Aparavi
 
Best Accounting Practice Management Software Guide for 2025
Best Accounting Practice Management Software Guide for 2025Best Accounting Practice Management Software Guide for 2025
Best Accounting Practice Management Software Guide for 2025
Tidyflow
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
The Elixir Developer - All Things Open
The Elixir Developer - All Things OpenThe Elixir Developer - All Things Open
The Elixir Developer - All Things Open
Carlo Gilmar Padilla Santana
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
How to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdfHow to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdf
victordsane
 
Navigating EAA Compliance in Testing.pdf
Navigating EAA Compliance in Testing.pdfNavigating EAA Compliance in Testing.pdf
Navigating EAA Compliance in Testing.pdf
Applitools
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Implementing promises with typescripts, step by step
Implementing promises with typescripts, step by stepImplementing promises with typescripts, step by step
Implementing promises with typescripts, step by step
Ran Wahle
 
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEMGDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
philipnathen82
 
Best Practices for Collaborating with 3D Artists in Mobile Game Development
Best Practices for Collaborating with 3D Artists in Mobile Game DevelopmentBest Practices for Collaborating with 3D Artists in Mobile Game Development
Best Practices for Collaborating with 3D Artists in Mobile Game Development
Juego Studios
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
saimabibi60507
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Microsoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptxMicrosoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptx
Mekonnen
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Streamline Your Manufacturing Data. Strengthen Every Operation.
Streamline Your Manufacturing Data. Strengthen Every Operation.Streamline Your Manufacturing Data. Strengthen Every Operation.
Streamline Your Manufacturing Data. Strengthen Every Operation.
Aparavi
 
Best Accounting Practice Management Software Guide for 2025
Best Accounting Practice Management Software Guide for 2025Best Accounting Practice Management Software Guide for 2025
Best Accounting Practice Management Software Guide for 2025
Tidyflow
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 

Sorting Technique

  • 2. ALGO - INSERTION SORT (N, A) A : Array N : Length of Array A[] Start Algo For i:=1 to N-1 do value := A[i] j := i-1 while j >= 0 and A[j] > value do A[j+1] := A[j] j := j-1 endwhile A[j+1] = value Endfor End Algo
  • 3. Program – Insertion Sort #include<iostream> using namespace std; main() { int A[6] = {6,5,4,3,2,1}; int N=6; int i,j,val; for(i=1;i<N;i++) { val=A[i]; j=i-1; while(j>=0 && A[j]>val) { A[j+1]=A[j]; j=j-1; } A[j+1]=val; } cout<<endl; for(int i = 0 ; i<N ; i++) cout<<A[i]<<" "; system("pause"); }
  • 5. Worst case analysis of Insertion Sort Apply Insertion Sort on the following Data: 7,6,5,4,3,2,1
  • 7. Program – Selection Sort #include<iostream> using namespace std; main() { int A[6] = {5,2,6,4,3,1}; int N=6; int i,sel=0,min,index,swap=0, temp; //Find smallest number and swap with selected index with each iteration of outer loop while(sel<N) { min = A[sel]; for(i = sel; i< N; i++) { if(A[i]<min) //if smaller number is found { min=A[i]; index=i; // save index of smaller number swap=1; // swapping required.. } } //swapping if(swap==1) { A[sel] = A[sel] + A[index]; // a = a+b A[index] = A[sel] - A[index]; // b = a - b A[sel] = A[sel] - A[index]; // a = a-b } sel++; swap=0; } for(int i = 0 ; i<N ; i++) cout<<A[i]<<" "; system("pause"); }
  • 8. Apply Selection Sort on the following Data: 7,6,5,4,3,2,1 Worst case analysis of Selection Sort
  • 9. Quick Sort 15 20 35 10 25 55 50 45 40 30 3015 20 10 25 35 55 50 45 40 3015 20 2510 4535 40 50 55 15 2010 25 35 4540 50 55 10 15 20 25 30 35 40 45 50 55 30
  • 10. MERGE SORT 15 , 20 , 35 , 10 , 25 , 55 , 50 , 45 15 20 10 35 25 55 45 50 10 15 20 35 25 45 50 55 10 15 20 25 35 45 50 55 GIVEN DATA