ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

几种常见排序算法

2022-06-08 10:34:57  阅读:150  来源: 互联网

标签:cout temp int 几种 ++ 算法 arrayLength array 排序


几种常见排序算法

使用语言及编译环境

  1. 使用语言:C/C++
  2. 编译环境:VS Code

冒泡排序

参照https://www.bilibili.com/video/BV1T4411A7Fy?p=7

  1. 原理:数组元素两两比较,交换元素,大元素往后放
    #include <iostream>
    
     using namespace std;
    
     int main()
     {
         int arrayLength = 6;
         int array[arrayLength] = {10,2,5,80,35,60};
    
         for (int i = 0; i < arrayLength; i++)
         {
             cout << array[i] << " ";
         }
         cout << endl;
    
         for (size_t i = 0; i < arrayLength-1; i++)
         {
             for (size_t j = 0; j < arrayLength-1-i; j++)
             {
                 if (array[j] > array[j+1])
                 {
                     int temp = array[j+1];
                     array[j+1] = array[j];
                     array[j] = temp;
                 }
    
             }
         }
    
         for (int i = 0; i < arrayLength; i++)
         {
             cout << array[i] << " ";
         }
         cout << endl;
    
         system("pause");
         return 0;
     }
    

选择排序

参照https://www.bilibili.com/video/BV1T4411A7Fy?p=8

  1. 原理:从0索引处开始,将该位置的元素依次和后面的元素进行比较,小的元素往前放
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int arrayLength = 6;
        int array[arrayLength] = {10,2,5,80,35,60};
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        for (size_t index = 0; index < arrayLength-1; index++)
        {
            for (size_t i = index+1; i < arrayLength; i++)
            {
                if (array[index] > array[i])
                {
                    int temp = array[index];
                    array[index] = array[i];
                    array[i] = temp;
                } 
            } 
        }
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        system("pause");
        return 0;
    }
    

直接插入排序

参照https://www.bilibili.com/video/BV1T4411A7Fy?p=9

  1. 原理:从1索引处开始,将后面的元素插入之前的有序列表中,使之仍保持有序
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int arrayLength = 6;
        int array[arrayLength] = {10,2,5,80,35,60};
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        for (size_t i = 1; i < arrayLength; i++)
        {
            for (size_t j = i; j > 0; j--)
            {
                if (array[j] < array[j-1])
                {
                    int temp = array[j];
                    array[j] = array[j-1];
                    array[j-1] = temp;
                }    
            }   
        }
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        system("pause");
        return 0;
    }
    

希尔排序

参照https://www.bilibili.com/video/BV1T4411A7Fy?p=10

  1. 原理:合理选取增量,一轮排序后,不断缩小增量进行插入排序,直到增量为1,排序就结束
  2. 直接插入排序相当于增量为1的希尔排序
  3. 增量的合理选择:克努特(Knuth)序列导入,h=3*h+1
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int arrayLength = 6;
        int array[arrayLength] = {10,2,5,80,35,60};
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        //克努特序列选取增量
        int spac = 1;
        while (spac <= arrayLength/3)
        {
            spac = spac*3+1;
        } 
        //cout << "spac: " << spac << endl;
    
        for (size_t h = spac; h > 0; h=(h-1)/3)
        {
            for (size_t i = h; i < arrayLength; i++)
            {
                for (size_t j = i; j > h-1; j-=h)
                {
                    if (array[j] < array[j-h])
                    {
                        int temp = array[j];
                        array[j] = array[j-h];
                        array[j-h] = temp;
                    }    
                }   
            }
        }   
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        system("pause");
        return 0;
    }
    

快速排序

参照https://www.bilibili.com/video/BV1at411T75o/?spm_id_from=333.788

  1. 思想:分治法,比大小,在分区
  2. 原理:从数组中取一个数,将比这个数大的全放到右边,比这个数小的全放到左边;对左右两个分区进行此步骤,直到最后分区只有一个数
  3. 步骤:选一个基准数,先从后往前找小于它的数,将这个数放到基准数的位置,然后在从前往后找大于等于它的数,将这个数放到上一个小于基准数的位置,直到左右两个寻找的指针重合,然后重复对左区和右区进行上述操作,直到分区只有一个元素的时候,就认为整个数组有序了
    #include <iostream>
    
    using namespace std;
    
    void quickSort(int *arr, int start, int end);
    
    int main()
    {
        int arrayLength = 6;
        int array[arrayLength] = {10,2,5,80,35,60};
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        quickSort(array, 0, arrayLength-1);
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        system("pause");
        return 0;
    }
    
    void quickSort(int *arr, int start, int end)
    {
        if (start>=end)
        {
            return;
        }
    
        int L = start;
        int R = end;
        int pivot = arr[L];
    
        while (L<R)
        {
            //由后往前找比它小的数
            while (L<R && arr[R]>=pivot)
            {
                R--;
            }
            if (L<R)
            {
                arr[L] = arr[R];
                L++;
            }
    
            //由前往后找比它大或者等于的数
            while (L<R && arr[L]<pivot)
            {
                L++;
            }
            if (L<R)
            {
                arr[R] = arr[L];
                R--;
            }
    
            //把基准数放到最后L和R重合的地方
            if (L>=R)
            {
                arr[L] = pivot;
            }   
        }
    
        //递归处理左区和右区
        quickSort(arr, start, L-1);
        quickSort(arr, L+1, end);
    }
    

归并排序

参照:https://www.bilibili.com/video/BV1Pt4y197VZ?spm_id_from=333.337.search-card.all.click

  1. 思想:先划分再合并
  2. 原理:将数组中每个元素分为一组,然后两两组合,保证每次组合出来的数组都是有序的,直到只剩下一组的时候,排序完成
    #include <iostream>
    
    using namespace std;
    
    void mergeSort(int *arr, int size);
    
    int main()
    {
        int arrayLength = 6;
        int array[arrayLength] = {10,2,5,80,35,60};
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        mergeSort(array, arrayLength);
    
    
        for (int i = 0; i < arrayLength; i++)
        {
            cout << array[i] << " ";
        }
        cout << endl;
    
        system("pause");
        return 0;
    }
    
    //合并
    void merge(int *arr, int *tempArr, int left, int mid, int right)
    {
        int l = left; //标记左半区第一个未排序元素
        int r = mid+1;//标记右半区第一个未排序元素
        int t_pos = left;//临时数组的下标
    
        //合并
        while ((l <= mid)&&(r <= right))
        {
            if (arr[l] <= arr[r])
            {
                tempArr[t_pos++] = arr[l++];
            }
            else
            {
                tempArr[t_pos++] = arr[r++];
            }
        }
    
        //合并左半区剩余的元素
        while (l <= mid)
        {
            tempArr[t_pos++] = arr[l++];
        }
    
        //合并右半区剩余的元素
        while (r <= right)
        {
            tempArr[t_pos++] = arr[r++];
        }
    
        //把临时数组的元素放入原数组
        while (left <= right)
        {
            arr[left] = tempArr[left];
            left++;
        }
    }
    
    //归并排序
    void mSort(int *arr, int *tempArr, int left, int right)
    {
        if (left<right)
        {
            int mid = (left+right)/2;
            mSort(arr, tempArr, left, mid); //递归划分左半区域
            mSort(arr, tempArr, mid+1, right); //递归划分右半区域
            merge(arr, tempArr, left, mid, right); //合并部分
        }
    
        return;
    }
    
    //入口函数
    void mergeSort(int *arr, int size)
    {
        int *tempArr = new int[sizeof(int)*size];
        if (tempArr != NULL)
        {
            mSort(arr, tempArr, 0, size-1);
            delete []tempArr;
        }
    
        return;
    }
    

后续会继续补充

标签:cout,temp,int,几种,++,算法,arrayLength,array,排序
来源: https://www.cnblogs.com/dai-xx/p/16354658.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有