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.

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