SlideShare a Scribd company logo
1 of 23
Sorting
Selection Sort
Umarfarooqworld@gmail.com
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i; P=0
temp
p=0
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=0
for(j=i+1;j<5;j++) j-=1
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=0
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=0
for(j=i+1;j<5;j++)
if(arr[p]>arr[j]) false
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=0
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
J=2
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=0
for(j=i+1;j<5;j++)
if(arr[p]>arr[j]) true
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
P=2
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
J=3
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j]) false
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
J=4
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j]) false
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
J=5
p=j;
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 1 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
Temp=1
p=j;
temp=arr[p];
temp
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
2 4 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
Arr[p]=2
p=j;
temp=arr[p];
temp
arr[p]=arr[i];
1
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
4 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
p=j;
temp=arr[p];
temp
arr[p]=arr[i];
1
2
arr[i]=temp; Arr[i]=1
Selection Sort
0 1 2 3 4
int arr[5]={2,4,1,9,7};
4 9 7
int p,i,j,temp;
for(i=0;i<5;i++)
{
p=i;
p=2
for(j=i+1;j<5;j++)
if(arr[p]>arr[j])
p=j;
temp=arr[p];
temp
arr[p]=arr[i];
1 2
arr[i]=temp;
Next iteration
}

More Related Content

More from Umar Farooq

Ado.Net Architecture
Ado.Net ArchitectureAdo.Net Architecture
Ado.Net ArchitectureUmar Farooq
 
Creating Windows-based Applications Part-I
Creating Windows-based Applications Part-ICreating Windows-based Applications Part-I
Creating Windows-based Applications Part-IUmar Farooq
 
Building .NET-based Applications with C#
Building .NET-based Applications with C#Building .NET-based Applications with C#
Building .NET-based Applications with C#Umar Farooq
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Umar Farooq
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#Umar Farooq
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)Umar Farooq
 
Software process model
Software process modelSoftware process model
Software process modelUmar Farooq
 

More from Umar Farooq (10)

Ado.Net Architecture
Ado.Net ArchitectureAdo.Net Architecture
Ado.Net Architecture
 
Linq in C#
Linq in C#Linq in C#
Linq in C#
 
Creating Windows-based Applications Part-I
Creating Windows-based Applications Part-ICreating Windows-based Applications Part-I
Creating Windows-based Applications Part-I
 
Building .NET-based Applications with C#
Building .NET-based Applications with C#Building .NET-based Applications with C#
Building .NET-based Applications with C#
 
Week 9 IUB c#
Week 9 IUB c#Week 9 IUB c#
Week 9 IUB c#
 
Matrix
MatrixMatrix
Matrix
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
 
Software process model
Software process modelSoftware process model
Software process model
 

Selection Sort

  • 2. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7};
  • 3. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7
  • 4. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; temp
  • 5. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) temp
  • 6. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { temp
  • 7. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; temp
  • 8. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; P=0 temp p=0
  • 9. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=0 for(j=i+1;j<5;j++) j-=1 temp
  • 10. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=0 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) temp
  • 11. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=0 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) false p=j; temp
  • 12. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=0 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) J=2 p=j; temp
  • 13. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=0 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) true p=j; temp
  • 14. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) P=2 p=j; temp
  • 15. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) J=3 p=j; temp
  • 16. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) false p=j; temp
  • 17. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) J=4 p=j; temp
  • 18. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) false p=j; temp
  • 19. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) J=5 p=j; temp
  • 20. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 1 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) Temp=1 p=j; temp=arr[p]; temp
  • 21. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 2 4 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) Arr[p]=2 p=j; temp=arr[p]; temp arr[p]=arr[i]; 1
  • 22. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 4 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) p=j; temp=arr[p]; temp arr[p]=arr[i]; 1 2 arr[i]=temp; Arr[i]=1
  • 23. Selection Sort 0 1 2 3 4 int arr[5]={2,4,1,9,7}; 4 9 7 int p,i,j,temp; for(i=0;i<5;i++) { p=i; p=2 for(j=i+1;j<5;j++) if(arr[p]>arr[j]) p=j; temp=arr[p]; temp arr[p]=arr[i]; 1 2 arr[i]=temp; Next iteration }