冒泡排序给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
相关推荐
To_OC4 小时前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
05Kevin18 小时前
lk每日冒险题--数据结构6.27
算法
To_OC1 天前
从一次栈溢出报错说起,我把递归彻底扒明白了
javascript·算法·程序员
千纸鹤安安1 天前
千问Qwen-AgentWorld来了:一个语言模型搞定七大Agent场景,GPT-5.4都输了
算法
七牛开发者1 天前
MCP 到底是什么?为什么 Agent 都想接上它
算法·aigc·agent
kisshyshy2 天前
从递归到迭代,一文吃透二叉树的核心知识与 JavaScript 实现
javascript·算法·代码规范
To_OC2 天前
LC 49 字母异位词分组:想到哈希表很简单,选对 key 才是精髓
javascript·算法·leetcode