zoukankan      html  css  js  c++  java
  • visualgo 各种算法的具体实现——排序篇

      某天发现一个神奇的网站https://visualgo.net/en,对于学习各个算法非常有用,它将算法的步骤可视化,能很好地帮助我们理解。

      顺序为从小到大。

      1,冒泡排序

      从头到尾两两比较,如果前者比后者大就交换,重复这个过程,直到不需要交换。

      visualgo伪代码:

      

    do
    
      swapped = false
    
      for i = 1 to indexOfLastUnsortedElement-1
    
        if leftElement > rightElement
    
          swap(leftElement, rightElement)
    
          swapped = true
    
    while swapped

      UE4中C++实现:

    void Atest::BubbleSort(TArray<int>& _array)
    {
        bool swapped = false;
        do 
        {
            swapped = false;
            for (int i = 0; i < _array.Num() - 1; i++)
            {
                if (_array[i] > _array[i + 1])
                {
                    int temp = _array[i];
                    _array[i] = _array[i + 1];
                    _array[i + 1] = temp;
                    swapped = true;
                }
            }
    
        } while (swapped);
    }

    2,选择排序

      从待排序序列中选中最小的元素,与待排序序列第一个元素交换,重复n-1次该过程。

      visualgo伪代码:

    repeat (numOfElements - 1) times
    
      set the first unsorted element as the minimum
    
      for each of the unsorted elements
    
        if element < currentMinimum
    
          set element as new minimum
    
      swap minimum with first unsorted position

      UE4中C++实现:

    void Atest::SelectSort(TArray<int>& _array)
    {
        for (int i = 0; i < _array.Num() - 1; i++)
        {
            int min = _array[i];
            int minIndex = i;
            for (int j = i; j < _array.Num(); j++)
            {
                if (_array[j] < min)
                {
                    min = _array[j];
                    minIndex = j;
                }
            }
            int temp = _array[i];
            _array[i] = _array[minIndex];
            _array[minIndex] = temp;
        }
    }

    3,快速排序

     //快速排序
        void QuickSort(int[] _sortArray)
        {
            qSort(_sortArray, 0, _sortArray.Length - 1);
        }
        void qSort(int[] _sortArray,int low,int high)
        {
            if (low < high)
            {
                int pivot = partition(_sortArray, low, high);        //将数组分为两部分
                qSort(_sortArray, low, pivot - 1);                   //递归排序左子数组
                qSort(_sortArray, pivot + 1, high);                  //递归排序右子数组
            }
        }
        int partition(int[] _sortArray,int low,int high)
        {
            int pivot = _sortArray[low];     //枢轴记录
            while (low < high)
            {
                while (low < high && _sortArray[high] >= pivot) --high;
                _sortArray[low] = _sortArray[high];             //交换比枢轴小的记录到左端
                while (low < high && _sortArray[low] <= pivot) ++low;
                _sortArray[high] = _sortArray[low];           //交换比枢轴小的记录到右端
            }
            //扫描完成,枢轴到位
            _sortArray[low] = pivot;
            //返回的是枢轴的位置
            return low;
        }

    4,插入排序

      插入排序算法有种递归的思想在里面,它由N-1趟排序组成。初始时,只考虑数组下标0处的元素,只有一个元素,显然是有序的。然后第一趟 对下标 1 处的元素进行排序,保证数组[0,1]上的元素有序;

    第二趟 对下标 2 处的元素进行排序,保证数组[0,2]上的元素有序;

        void InsertionSort(int[]  _sortArray)
        {
            for(int i = 1;i < _sortArray.Length;++i)
            {
                int temp = _sortArray[i];
                int j;
                for(j = i;j < _sortArray.Length && temp < _sortArray[j-1];--j)
                {
                    _sortArray[j] = _sortArray[j - 1];
                }
                _sortArray[j] = temp;
            }
        }
  • 相关阅读:
    Docker PHP 扩展配置
    PHP 打水印功能
    MYSQL的主从复制
    CentOS7开启防火墙及特定端口
    非常全面的讲解Hosts文件
    STL的erase()陷阱-迭代器失效总结
    scons使用
    SecureCRT 使用技巧
    atomic, spinlock and mutex性能比较
    内存池的实现(二)
  • 原文地址:https://www.cnblogs.com/litmin/p/8409948.html
Copyright © 2011-2022 走看看