SELECTION SORT USING C PROGRAM





Source code of simple Selection sort implementation using array ascending order in c programming language

#include<stdio.h>
int main(){

  int s,i,j,temp,a[20];

  printf("Enter total elements: ");
  scanf("%d",&s);

  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);

  for(i=0;i<s;i++){
      for(j=i+1;j<s;j++){
           if(a[i]>a[j]){
               temp=a[i];
              a[i]=a[j];
              a[j]=temp;
           }
      }
  }

  printf("After sorting is: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);

  return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is:  0 4 5 7 21






5. Write a c program for heap sort.
7. Write a c program for shell sort.

58 comments:

Unknown said...

how s=j? mens how it can be possible

Unknown said...

array= {8,10,12,19,5,2}
pass1 = {2,10,12,19,8,5}
pass2 = {2,5,12,19,10,8}
pass3 ={2,5,8,19,12,10}
pass4={2,5,8,10,19,12}
pass5={2,5,8,10,12,19}
Check selection sort example is Correct or not and give me ans

Anonymous said...

no the above example is wrong as it is the coding for bubble sort

Saad Asif said...

I can't see the step involving the selection of the lowest element of the array. This is whatever but definitely not seletion sort

Anonymous said...

this is a selection sort...

Anonymous said...

Hey this is bubble sort
not selection sort

Unknown said...

array= {8,10,12,19,5,2}
pass1 = {2,10,12,19,8,5}
pass2 = {2,5,12,19,10,8}
pass3 ={2,5,8,19,12,10}
pass4={2,5,8,10,19,12}
pass5={2,5,8,10,12,19}
its wrong
right is-
array= {8,10,12,19,5,2}
array= {2,10,12,19,5,8}
array= {2,5,12,19,10,8}
array= {2,5,8,19,10,12}
array= {2,5,8,10,19,12}
array= {2,5,8,10,12,19}

A=Myth said...

I think selection sort works on its POSITION and sort element on locations...

my solution is....

#include
void main()
{
int arr[5]={3,10,2,4,1};
int min,i,j,temp;
clrscr();
for(i=0;i<5;i++)
{
min=i;
for(j=i+1;j<5;j++)
{
if(arr[j]<arr[min])
min=j;
}
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;

}


for(i=0;i<5;i++)
printf("\n\n\t%d",arr[i]);
getch();
}

Hope its correct not...if YES then ADMIN plz update ur prog logic...

Anonymous said...

Source code of simple quick sort implementation using array ascending order in c programming language



#include

void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Enter size of the array: ");
scanf("%d",&size);

printf("Enter %d elements: ",size);
for(i=0;ix[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8

Anonymous said...

this is not slecction sort

Anonymous said...

THIS IS SELECTION SORTING GUYS..
BUBBLE IS DIFFERENT -comparing each with adjacent element.
This is different-after inner loop is executed once..the first element is the smallest. SO this is selection sorting

--mithun

Anonymous said...

why does dis code need 5 variables 3 are enuf ...

Anonymous said...

plzzz i need algorithm for the first program

Anonymous said...

Any how, your effort is very very worthful...
Thanks a lot... :) :)

Anonymous said...

#include
#include
void main()
{
int n,a[10],i,j,temp,pos=0,small;
clrscr();
printf("enter the size of array \n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d"&a[i]);
}
for(j=0;j<n;j++)
{
if(a[j]<a[pos])
{
pod=j;
}
}
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
}
printf("the sorted elements are \n");
for(i=0;i<n;i++)
{
printf("%d\n",&a[i]);
}
getch();
}

Unknown said...

M ADDING A PROGRAM WHICH IMPLEMENTS SELECTION, INSERTION AND
BUBBLE SORT BUT IT IS SHOWING SYNTAX ERROR WEN M COMPILING IT IN DEV C++ AND CODE BLOCKS ... PLZ REPLY ASAP
#include
#include
void bubble( int a[5],i,j,n,t,s)
{
int i,j,n,t,s;
printf("Enter the array:");
for(i=0;i<5;i++)
{
scanf("\n%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=0;j<5-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
printf("\n Sorted array:");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}
}
void insertion(int a[5])
{
int i,n,j,temp;
printf("Enter the array:");
for(i=0;i=0;j--,i--)
{
if(a[i]a[j])
temp=a[j];
a[j]=a[i];
a[i]=temp;


}
}
}
printf("\n\nArray aftr sort:");
for(i=0;i<n;i++)
printf("%d",a[i]);

}
main()
{

int choice;
while(1)
{
printf("\n");
printf("\n1. Insertion \n");
printf("\n2.Bubble\n");
printf("\n3. Selection");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1: bubble(int a[5],i,j,n,t,s);
break;
case 2: insertion(int a[5],i,j,n,temp);
break;
case 3:selection(int a[5],i,j,n,temp);
break;
default:printf("wrong choice\n");
}

}getch();
}

Unknown said...

this is not the code of a selection sort.it is abubble sort program code.

Anonymous said...

I want c++ program to implement linked list class using Selection Sort recursively,

thanks

Anonymous said...

how can we merge to array using merge sort after sorting them using selection sort

Anonymous said...

great

Anonymous said...

its bubble sort technique and u using extra steps for printing sorted array.

Anonymous said...

ya its working correctly for above elements...

Anonymous said...

its bubble sort technique

Mohan said...
This comment has been removed by the author.
Anonymous said...

1. Psuedo code for insertion sorting
INSERTION-SORT(a)
for j = 2 to n
key =A [j]
// Insert A[j] into the sorted sequence A[1..j-1]
j = i – 1
while i > 0 and A[i] > key
A[i+1]= A[i]
i = i – 1
A[j+1]=key
2. 2 . Psuedo code for linked list
Procedure Create(Value)
If there is enough memory available then
Create New Node in memory
Point its Next pointer to NULL
If this is the first Node in the List then
Point Start to the Node
Otherwise
Go to memory location pointed by Start pointer
Start Loop and run until Next points to
Go to the Node pointed by Next pointer
End Loop
Point Next of last Node to the newly created Node
Put data in Value in new Node's Data
Endif
Otherwise
Output error message that there is no more memory available
EndIf
End Procedure
Procedure Delete(Value)
Pointer : Found
Found = NULL
If there are no Nodes in the List then
Output error message that can't delete a Node
Otherwise
Go to memory location pointed by Start pointer
Start Loop and run until Next points to NULL
If value in Data of current Node is equal to Value then
Found = current Node
Exit Loop
Endif
Go to Node pointed by Next pointer of current Node
End Loop
If the required Node is the first Node in the List then
Point the Start pointer to NULL
Otherwise
Point Next of previous Node to the Node pointed by Next of Found
Endif
Endif
End Procedure
Procedure Display
If Start points to NULL then
Output error message that the List is empty
Otherwise
Start Loop at Start position and run until Next points to NULL
Output current Node's Data
Go to Node pointed by Next pointer of current Node
End Loop
Endif
End Procedure
3. Psuedo code for queue

Procedure Insert(Value)
If End is equal to MAX then
Output an error message that the Queue is full
Otherwise
Add 1 to End 'Go to next available element in Queue
Store data in Value into Queue at End position
Endif
End Procedure
Procedure delete(Value)
If End is equal to 0 then
Output an error message that the Queue is empty
Otherwise
SUB 1 from End 'Go to previous available element in Queue
Remove data in Value into Queue at End position
Endif
End Procedure
Procedure Display
If End is equal to Zero then
Output an error message that the Queue is empty
Otherwise
Display value at position Queue[1]
Start Loop from position 1 to End position
Copy data from Next element to Current element
Move to Next element
End Loop
EndIf
End Procedure

Unknown said...

plz write selection sort code in c using a seperate swap function.

Anonymous said...

what is the use void main instead of int main ?? i dont understand... can u please explain to me sir.. :)

Unknown said...

above program is wrong it is actually bubble sort

Anonymous said...

buble sort

Unknown said...

correct program is this
#include
#include

void main()
{
int a[100];
int size,i,n,si=0,sj=0,min=0,temp;

printf("\n-----------------------------------"
"\n Selection Sort "
"\n-----------------------------------");
printf("\nEnter size of Array");
scanf("%d",&size);

printf("\nEnter Elements");
for(i=0;i<size;i++)
{
scanf("%d",&n);
a[i]=n;
}

for(si=0;si<size;si++)
{
min=si;
for(sj=si+1;sj<size;sj++)
{
if(a[sj]<a[min])
min=sj;
}
temp=a[min];
a[min]=a[si];
a[si]=temp;
}

for(i=0;i<size;i++)
{
printf("%d ", a[i] );
}
getch();
}

Italics121 said...

What this means "(j=i+1;j<s;j++)"?

sys101jimi said...

Int main(...
return 4;
} // returns a condition code of 4 to the invoker
void main(...
return;
} //return nothing
// but most implimentations return 0 unless your system detects fatal error

Ramf966 said...

yes, correct it is:D

Unknown said...

i need a c program that accept values from user,arrange in ascending sorting oreder...condition is that when we enterd a number..the next number should be greater than first..like if we entered 20 than next will be greater..if smaller than program should stop..plz help me..

Unknown said...

SELECTION SORT

Write a C program to perform selection sort on an array of n elements.

Input Format:
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next n integers correspond to the elements in the array.

Output Format:
Refer sample output for formatting specs.

Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]

Enter the number of elements in the array

6

Enter element 1

2

Enter element 2

7

Enter element 3

3

Enter element 4

8

Enter element 5

5

Enter element 6

1

Selection sort.

array before sorting:

2 7 3 8 5 1

After Iteration 1

1 7 3 8 5 2

After Iteration 2

1 2 7 8 5 3

After Iteration 3

1 2 3 8 7 5

After Iteration 4

1 2 3 5 8 7

After Iteration 5

1 2 3 5 7 8

After Iteration 6

1 2 3 5 7 8

array after sorting:

1 2 3 5 7 8






Unknown said...

how to print the array after each iteration for selection sort??.........
can anybody suggest me some idea?????????

Unknown said...

MP 5 – Take Home!
Write a program that asks the user to type 10 integers of an array.
The program must compute and
write how many integers are greater than or equal to 10.
Sample Input/Output:
Enter number 1: 8
Enter number 2: 11
Enter number 3: 1
Enter number 4: 12
Enter number 5: 20
Enter number 6: 5
Enter number 7: 6
Enter number 8: 9
Enter number 9: 100
Enter number 10: 21
the number of integers greater or equal to 10 is 5.

Unknown said...

please answer guys
array java programming eclipse
Please

Unknown said...

i need answer that please

Unknown said...

please answer guys
array java programming eclipse
Please

fuckatfusnsö said...

This cannot be selection sort. The only line that differs from this and the bubble sort example provided on this website is the first for loop.

Unknown said...

I Do Ge The Result Which Button

First How Elements
Then Elements
So I Do Get The Result

Unknown said...

Which Button Give Me The Result Of Sorting?

Unknown said...

Solve this using selection sort
12, 55, 77, 90, 40, 60, 99, 22, 88, 66

Unknown said...

Solve this using selection sort
12, 55, 77, 90, 40, 60, 99, 22, 88, 66

Unknown said...

Please solve this for me....A program that gets five inputs from the user in an array and then sort this array in ascending order

Unknown said...

Please solve this for me....A program that gets five inputs from the user in an array and then sort this array in ascending order

Unknown said...

Can you please tell me if I don't change the 3rd number just sorting the others, what is the techque?

Unknown said...

Can you please tell me if I don't change the 3rd number just sorting the others, what is the techque?

Unknown said...

WRONG!!!This is not a program for selection sort.It is for bubble sort.

Unknown said...

Yes you is correct.

Unknown said...

SELECTION SORT

Write a C program to perform selection sort on an array of n elements.

Input Format:
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next n integers correspond to the elements in the array.

Output Format:
Refer sample output for formatting specs.

Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]

Enter the number of elements in the array

6

Enter element 1

2

Enter element 2

7

Enter element 3

3

Enter element 4

8

Enter element 5

5

Enter element 6

1

Selection sort.

array before sorting:

2 7 3 8 5 1

After Iteration 1

1 7 3 8 5 2

After Iteration 2

1 2 7 8 5 3

After Iteration 3

1 2 3 8 7 5

After Iteration 4

1 2 3 5 8 7

After Iteration 5

1 2 3 5 7 8

After Iteration 6

1 2 3 5 7 8

array after sorting:

1 2 3 5 7 8

Unknown said...

plzz I need this program
SELECTION SORT

Write a C program to perform selection sort on an array of n elements.

Input Format:
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next n integers correspond to the elements in the array.

Output Format:
Refer sample output for formatting specs.

Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]

Enter the number of elements in the array

6

Enter element 1

2

Enter element 2

7

Enter element 3

3

Enter element 4

8

Enter element 5

5

Enter element 6

1

Selection sort.

array before sorting:

2 7 3 8 5 1

After Iteration 1

1 7 3 8 5 2

After Iteration 2

1 2 7 8 5 3

After Iteration 3

1 2 3 8 7 5

After Iteration 4

1 2 3 5 8 7

After Iteration 5

1 2 3 5 7 8

After Iteration 6

1 2 3 5 7 8

array after sorting:

1 2 3 5 7 8

Unknown said...

The above mentioned code is not of Selection Sort instead it is of Bubble Sort as in Bubble sort we compare the adjacent elements and so is the implementation.

Unknown said...

The above mentioned code is not of Selection Sort instead it is of Bubble Sort as in Bubble sort we compare the adjacent elements and so is the implementation.

Unknown said...

import java.util.Arrays;


public class SelectionSort {
public static void main(String[] args) {
int a[]= new int[]{10,11,23,5,6,15};
for(int i=0;ia[j]){
int temp=0;
temp=a[j];
a[j]=a[i];
a[i]=temp;

}

}
}
System.out.println("Sorted list:"+Arrays.toString(a));
}
}

Unknown said...

S is.the total no elements inn the. Array..

mehak aggarwal said...

How its work correctly plz tell me