Skip to content

Instantly share code, notes, and snippets.

@CraftedPvP
Last active June 1, 2017 11:26
Show Gist options
  • Save CraftedPvP/c9e320b50f041cf7cbcc96c1e5e732c7 to your computer and use it in GitHub Desktop.
Save CraftedPvP/c9e320b50f041cf7cbcc96c1e5e732c7 to your computer and use it in GitHub Desktop.

Revisions

  1. CraftedPvP revised this gist Jun 1, 2017. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions Sorting algorithms.cpp
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,8 @@ void insertion_sort (int arr[], int length){
    }

    //
    //---Quick Sort - Sorts the data by pairs by getting the minimum and swapping it with the current---//
    //--- Link: http://www.algolist.net/Algorithms/Sorting/Quicksort ---//
    //---Quick Sort - Sorts the data by pairs by getting the minimum end then swapping it with the current---//
    //--- Link: http://www.algolist.net/Algorithms/Sorting/Quicksort ---//
    //
    void quick_sort(int arr[], int left, int right) {
    int i = left, j = right;
    @@ -49,12 +49,14 @@ void quick_sort(int arr[], int left, int right) {
    //--- Bubble Sort - Swap adjacent pairs. If it's not in the wrong order then swap them. ---//
    //--- Link: http://www.thecrazyprogrammer.com/2011/11/c-program-to-sort-array-by-using-bubble.html ---//
    //
    for(i=1;i<n;++i) {
    for(j=0;j<(n-i);++j){
    if(a[j]>a[j+1]){
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    int size=5, temp;
    int container[size];
    for(int i=1;i<size;++i) {
    for(int j=0;j<(size-i);++j){
    if(container[j]>container[j+1]){
    temp=container[j];
    container[j]=container[j+1];
    container[j+1]=temp;
    }
    }
    }
  2. CraftedPvP revised this gist Jun 1, 2017. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions Sorting algorithms.cpp
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,15 @@
    //--- Link: http://cforbeginners.com/insertionsort.html ---//
    //
    void insertion_sort (int arr[], int length){
    int j, temp;
    int j, temp;
    for (int i = 1; i < length; i++){
    j = i;
    while (j > 0 && arr[j] < arr[j-1]){
    temp = arr[j];
    arr[j] = arr[j-1];
    arr[j-1] = temp;
    j--;
    }
    while (j > 0 && arr[j] < arr[j-1]){
    temp = arr[j];
    arr[j] = arr[j-1];
    arr[j-1] = temp;
    j--;
    }
    }
    }

  3. CraftedPvP created this gist Jun 1, 2017.
    60 changes: 60 additions & 0 deletions Sorting algorithms.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    //
    //---Insertion Sort - Sorts the data by pairs then checks j from those behind it---//
    //--- Link: http://cforbeginners.com/insertionsort.html ---//
    //
    void insertion_sort (int arr[], int length){
    int j, temp;
    for (int i = 1; i < length; i++){
    j = i;
    while (j > 0 && arr[j] < arr[j-1]){
    temp = arr[j];
    arr[j] = arr[j-1];
    arr[j-1] = temp;
    j--;
    }
    }
    }

    //
    //---Quick Sort - Sorts the data by pairs by getting the minimum and swapping it with the current---//
    //--- Link: http://www.algolist.net/Algorithms/Sorting/Quicksort ---//
    //
    void quick_sort(int arr[], int left, int right) {
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];
    //partition
    while (i <= j) {
    while (arr[i] < pivot)
    i++;
    while (arr[j] > pivot)
    j--;
    if (i <= j) {
    tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
    i++;
    j--;
    }
    };

    /* recursion */
    if (left < j)
    quickSort(arr, left, j);
    if (i < right)
    quickSort(arr, i, right);
    }

    //
    //--- Bubble Sort - Swap adjacent pairs. If it's not in the wrong order then swap them. ---//
    //--- Link: http://www.thecrazyprogrammer.com/2011/11/c-program-to-sort-array-by-using-bubble.html ---//
    //
    for(i=1;i<n;++i) {
    for(j=0;j<(n-i);++j){
    if(a[j]>a[j+1]){
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
    }
    }