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

编程入门 行业动态 更新时间:2024-10-22 19:32:40

冒泡排序给cpu干懵了 哈哈 还有<a href=https://www.elefans.com/category/jswz/34/1769823.html style=希尔排序 算法补充(学习笔记)"/>

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

直接给出代码 

#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 arrayfor( 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;}

下面是单独的冒泡排序 

(C%2B%2B)/Optional-03-Shell-Sort/main.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

更多推荐

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

本文发布于:2023-12-07 11:19:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1671093.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:希尔   算法   学习笔记   懵了   cpu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!