【C++算法笔记】Sort的六种方法 【一】

IMPORTANT

THIS IS NOTE I MADE WHEN STUDY C++ ALGORITHM
THIS NOTE WOULD ONLY HELP U SPEND LESS TIME ON UNDERSTNDING THE ALGORITHM AND CODE IMPLEMENTATION
ALL WORDS IN " " ARE DIRECTLY REFERENCE
ALL CODE IMPLEMATATION ARE DIRECTLY REFERENCE
PLEASE CLICK THE LINK BELOW TO READ ORIGINAL BOOK AND INSTRUCTION

https://cathyatseneca.gitbook.io/data-strutures-and-algorithms/


Bubble sort
  • "Bubble sort is called bubble sort because the algorithm repeated bubbles the largest item within the list to the back.
    "
void bubbleSort(int arr[],int size){
    int tmp;                         
    int i;
    int j;
    for (i=0; i<size-1; i++) {
        for (j=0; j<size-1-i; j++){
            if (arr[j+1] < arr[j]) {          
                tmp = arr[j];                  
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
            }
        }
    }
}

In this method, the largest number will be bubble to the end.
Any number go throught the two for-loop process considers as sorted;
and will stores in the end of the index of array.
The first for-loop is to ensure sorting process go through all the array;
the second for-loop is to ensure the size of unsorted array.


Insertion Sort
  • “Insertion sort is called insertion sort because the algorithm repeated inserts a value into an array that is already sorted. It essentially chops the array into two pieces. The first piece is sorted, the second is not. We repeatedly take a number from the second piece and insert it into the already sorted first piece of the array.”
void insertionSort(int arr[],int size){
    int curr;
    int i, j;
    for(i=1;i<size;i++){
        curr=arr[i];
        for(j=i;j>0 && arr[j-1] > curr;j--){
            arr[j]=arr[j-1];
        }
        arr[j]=curr;
    }
}

In this method, the sorted part always locates in the begining of index.
A flag will creates to identify the sorted part and unsorted part;
and also represents data from current index.
The first loop will ensure sorting process tunning to the end;
noticed that i start with 0, coz it always compare with the index in front of i;
also, the value of flag changes with i;
the second for loop will ensure the new added number locates in correct position,
to do this,
it will compare the new added number with the largest element in the sorted part, which inital index is i,
if the number is smaller,
it will keep compare the the further number until finding correct position.
Noticed that, instead of swap or switch the indexs between two unfitted number, the only operation inside the second loop is to push the larger number to larger index;
we are going to sign the new added number into correct index goes out of the second loop.


Selection Sort
  • “Selection sort works by selecting the smallest value out of the unsorted part of the array and placing it at the back of the sorted part of the array.”
void selectionSort(int arr[],int size){
    int i,j;
    int min; 
    for(int i=0;i<size-1;i++){
        min=i;
        for(int j=i+1;j<size;j++){
            if(arr[j] < arr[min]){
                min=j;
            }
        }
        if(min!=i){
            int tmp=arr[min];
            arr[min]=arr[i];
            arr[i]=tmp;
        }
    }
}

In this method, the smallest number will be continuously selected and located in the beginning of the array, so does the sorted part.
A temp variable is needed to store the temporary smalllest numebr in the unsorted list;
The firtt for-loop ensure process going to the end,
switch two numbers if find a smaller one;
the second loop compare the largest number in the sorted part with the rest of unsorted part,
every round needs going to the end,
to make sure temp stores the smellest one.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值