Skip to content

Instantly share code, notes, and snippets.

@saifsmailbox98
Created December 27, 2018 18:21
Show Gist options
  • Select an option

  • Save saifsmailbox98/b67d819f9c18f06403f8bf5da32da36a to your computer and use it in GitHub Desktop.

Select an option

Save saifsmailbox98/b67d819f9c18f06403f8bf5da32da36a to your computer and use it in GitHub Desktop.

Revisions

  1. saifsmailbox98 created this gist Dec 27, 2018.
    50 changes: 50 additions & 0 deletions sort.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    void swap(int&a, int&b) {
    int temp;
    temp = a;
    a = b;
    b = temp;
    }

    void display(int a[], int n) {
    for (int i = 0; i < n; i++)
    printf("%d ", a[i]);
    printf("\n");
    }

    void bubble_sort(int A[], int n){
    for(int k=0; k<n-1; k++){
    for(int i=0; i<n-k-1; i++){
    if(A[i]>A[i+1]){
    swap(A[i], A[i+1]);
    }
    }
    }
    }

    void selection_sort(int A[], int n) {

    int min;

    for(int i=0; i<n-1; i++){
    min = i;
    for(int j=i+1; j<n; j++){
    if(A[j]<A[min]){
    min = j;
    }
    }
    swap(A[min], A[i]);
    }
    }

    void insertion_sort(int A[], int n)
    {
    for(int i=0; i<n; i++){
    int temp = A[i];
    int j = i;
    while(j>0&&temp<A[j-1]){
    A[j] = A[j-1];
    j--;
    }
    A[j] = temp;
    }
    }