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.

50 comments:

  1. how s=j? mens how it can be possible

    ReplyDelete
  2. 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

    ReplyDelete
    Replies
    1. ya its working correctly for above elements...

      Delete
    2. 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.

      Delete
    3. please answer guys
      array java programming eclipse
      Please

      Delete
    4. please answer guys
      array java programming eclipse
      Please

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

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

    ReplyDelete
  5. this is a selection sort...

    ReplyDelete
    Replies
    1. 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..

      Delete
  6. Hey this is bubble sort
    not selection sort

    ReplyDelete
  7. 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}

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

      Delete
  8. 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...

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

      Delete
    2. 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

      Delete
  9. 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

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

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

      Delete
  10. this is not slecction sort

    ReplyDelete
  11. 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

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

    ReplyDelete
  13. plzzz i need algorithm for the first program

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

    ReplyDelete
  15. #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();
    }

    ReplyDelete
  16. 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();
    }

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

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

    thanks

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

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

    ReplyDelete
  21. its bubble sort technique

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. 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

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

    ReplyDelete
  25. above program is wrong it is actually bubble sort

    ReplyDelete
  26. buble sort

    ReplyDelete
  27. 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();
    }

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

    ReplyDelete
  29. 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






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

    ReplyDelete
  31. 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.

    ReplyDelete
  32. I Do Ge The Result Which Button

    First How Elements
    Then Elements
    So I Do Get The Result

    ReplyDelete
  33. Which Button Give Me The Result Of Sorting?

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

    ReplyDelete
  35. 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

    ReplyDelete
  36. 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

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

    ReplyDelete