博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重拾数据结构之排序
阅读量:7037 次
发布时间:2019-06-28

本文共 1591 字,大约阅读时间需要 5 分钟。

1、Bubble Sort Implementation (冒泡排序) Solution:

void bubbleSort(int arr[], int n){    for ( int i =0; i < n - 1; i++){              for (int j = 0; j < n - 1 - i; j++){                      if (arr[j] > arr[j + 1]){                __swap(arr[j], arr[j + 1]);            }        }    }}复制代码

2、Selection Sort Implementation (选择排序) Solution:

void selectionSort(int arr[], int n){    for ( int i = 0; i < n; i++){        int min = i;        for ( int j = i + 1; j < n; j++){            if (arr[min] > arr[j]){                min = j;            }        }        if (min != i){            __swap(arr[i], arr[min]);        }    }}复制代码

3、Insertion Sort Implementation(插入排序) Solution:

void insertionSort(int arr[], int n){    for ( int i = 1; i < n; i ++){        for (int j = i; j > 0; j --){            if (arr[j] < arr[j - 1]){                __swap(arr[j], arr[j-1]);            }else{                break;            }        }    }}复制代码

4、Quick Sort Implementation (快速排序)

int __partition2Way(int arr[], int l , int r){        //pivot is the random element in array    swap(arr[l], arr[rand()%(r-l+1)+l]);    int pivot = arr[l];        int i = l + 1, j = r;        //l+1,i    j,r    while (true) {                while (i <= r && arr[i] < pivot) {            i++;        }                while (j >= l + 1 && arr[j] > pivot) {            j--;        }                if (i > j) {            break;        }               swap(arr[i], arr[j]);                i++;        j--;    }        swap(arr[l], arr[j]);          return j;}复制代码

排序:https://blog.csdn.net/u010926964/article/details/40045501

你可能感兴趣的文章
音视频解决方案-如何开通视频点播
查看>>
Ubuntu安装虚拟机
查看>>
gitlab的搭建及问题的解决
查看>>
Cocos2d-x3.0模版容器详解之二:cocos2d::Map<K,V>
查看>>
免费获取WP之类的开发者权限或免费使用Azure 2015-10-19
查看>>
一步一步学lucene——(第三步:索引篇)
查看>>
js里cookie操作
查看>>
【原创】开源Math.NET基础数学类库使用(06)直接求解线性方程组
查看>>
JAVA学习(九):JAVA多线程编程
查看>>
基于Solr DIH实现MySQL表数据全量索引和增量索引
查看>>
Struts2--Helloworld
查看>>
【Java】基础语法
查看>>
AutoMySQLBackup 3.0 Bug:"du: WARNING: use --si, not -H"
查看>>
使用js Math.random()函数生成n到m间的随机数字
查看>>
[翻译] IDMPhotoBrowser
查看>>
wordpress 插件推荐
查看>>
IOS开发UI篇—导航控制器属性和基本使用
查看>>
android smartbar适配
查看>>
postgresql 分区与优化
查看>>
数码相框项目之LCD模块
查看>>