冒泡排序给cpu干懵了 哈哈 还有希尔排序 算法补充(学习笔记)

直接给出代码

cpp 复制代码
#include<iostream>
#include<string>
#include "Student.h"
#include "sorttesthelper.h"
#include "BubbleSort.h"
using namespace std;


template<typename T>
void shellSort(T arr[], int n){

    // 计算 increment sequence: 1, 4, 13, 40, 121, 364, 1093...
    int h = 1;
    while( h < n/3 )
        h = 3 * h + 1;

    while( h >= 1 ){

        // h-sort the array
        for( int i = h ; i < n ; i ++ ){

            // 对 arr[i], arr[i-h], arr[i-2*h], arr[i-3*h]... 使用插入排序
            T e = arr[i];
            int j;
            for( j = i ; j >= h && e < arr[j-h] ; j -= h )
                arr[j] = arr[j-h];
            arr[j] = e;
        }

        h /= 3;
    }
}

template<typename T >

void selectionSort( T arr[], int n){
    for(int i = 0 ; i < n ; i++){

        //寻找【i,n之间的最小值】
        int minIndex = i;
        for( int j = i + 1 ; j < n ; j++)
            if(arr[j] < arr[minIndex] )
                minIndex = j;

        swap( arr[i] , arr[minIndex]);

    }
}

//对插入排序来说,直接从第二个元素开始

template<typename T >
void InsertSort( T arr[], int n){
    for(int i = 1 ; i < n ; i++){

        T e = arr[i];

        // j 需要保存元素e应该插入的位置
        int j;
        //寻找【i应该插入的位置】,但是注意我们是从后面往前找所以j 要从后往前
        
        // for( int j = i  ; j > 0 ; j --)
        //     if(arr[j] < arr[j - 1] )
        //         swap(arr[j], arr[j-1]);
        //     else 
        //         break;
        //插入排序的优点是 可以提前 终止循环 所以对于几乎有序的序列 插入排序的性能非常强
        for( j = i  ; j > 0 && arr[j-1] > arr[e]; j --)
                arr[j] = arr[j-1];
        arr[j] = arr[e];
            

       

    }
}


int main()
{
    int a[5] = {5,62,3,58,44};
    selectionSort( a, 5 );
    for( int i = 0 ; i < 5 ; i++)
        cout<<a[i]<< " ";
    
    cout<<endl;
    
    float b[4] = {4.4,2.3,5.63};
    selectionSort( b , 3);
    for( int i = 0 ; i < 3 ; i++)
        cout<<b[i]<< " ";
    cout<<endl;

    string c[2] = {"z","b"};
    selectionSort( c , 2);
    for( int i = 0 ; i < 2 ; i++)
        cout<<c[i]<< " ";
    cout<<endl;

    Student d[3] = {{"D",90} , {"C",89} , { "B", 114}};
    selectionSort( d , 3);
    for( int i = 0 ; i < 3 ; i++)
        cout<<d[i];
    cout<<endl;


    int n = 100000;
    int *arr1 = SortTestHelper :: generateRandomArr(n, 0, n) ;
    int *arr2 = SortTestHelper :: copyIntArray(arr1, n);
    int *arr3 = SortTestHelper :: generateNearlyorderedArr(n, 100);

    int *arr4 = SortTestHelper::copyIntArray(arr1, n);
    int *arr5 = SortTestHelper::copyIntArray(arr1, n);
    // InsertSort(arr2, n);
    // SortTestHelper :: printarr(arr2, n);
    // selectionSort( arr, n );
    // SortTestHelper :: printarr(arr, n);
    // SortTestHelper::test_sort("selection Sort", selectionSort, arr,n);
    SortTestHelper::test_sort("Insertion Sort", InsertSort, arr2,n);
    SortTestHelper::test_sort("Insertion Sort a nearly ordered arr", InsertSort, arr3,n);
    SortTestHelper::test_sort("Bubble Sort", bubbleSort, arr4, n);
    SortTestHelper::test_sort("Shell Sort", shellSort, arr5, n);

    delete[] arr1;
    delete[] arr2;
    delete[] arr3;
    delete[] arr4;
    delete[] arr5;
    
    

    return 0;

}

下面是单独的冒泡排序

https://github.com/liuyubobobo/Play-with-Algorithms/blob/master/02-Sorting-Basic/Course%20Code%20(C%2B%2B)/Optional-03-Shell-Sort/main.cpp

给出大佬的链接

cpp 复制代码
//
// Created by liuyubobobo on 7/15/16.
//

#ifndef OPTIONAL_02_SHELL_SORT_BUBBLESORT_H
#define OPTIONAL_02_SHELL_SORT_BUBBLESORT_H

#include <iostream>
#include <algorithm>

using namespace std;

template<typename T>
void bubbleSort( T arr[] , int n){

    int newn; // 使用newn进行优化

    do{
        newn = 0;
        for( int i = 1 ; i < n ; i ++ )
            if( arr[i-1] > arr[i] ){
                swap( arr[i-1] , arr[i] );

                // 记录最后一次的交换位置,在此之后的元素在下一轮扫描中均不考虑
                newn = i;
            }
        n = newn;
    }while(newn > 0);
}

#endif //OPTIONAL_02_SHELL_SORT_BUBBLESORT_H
相关推荐
Coovally AI模型快速验证2 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
pusue_the_sun2 小时前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
RaymondZhao343 小时前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
zhangfeng11333 小时前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类
啊阿狸不会拉杆4 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路4 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
你知道网上冲浪吗5 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
地平线开发者6 小时前
征程 6 | PTQ 精度调优辅助代码,总有你用得上的
算法·自动驾驶
Tisfy7 小时前
LeetCode 837.新 21 点:动态规划+滑动窗口
数学·算法·leetcode·动态规划·dp·滑动窗口·概率
CoovallyAIHub7 小时前
为高空安全上双保险!无人机AI护航,YOLOv5秒判安全带,守护施工生命线
深度学习·算法·计算机视觉