第四篇、C_快速、冒泡、选择、插入排序、二分查找排序、归并、堆排序
1.快速排序
实现:
1.取中间一个数作为支点
2.分别在支点的左右两边进行查找,如果左边查找到比支点大,右边查找到比支点小,就交换位置,如此循环,比支点小的数就排在了左边,比支点大的就排在右边
3.左右两边再用递归排序,就可以完成排序操作

/**
*@brief 快速排序
*@params arr 数组 left 起始位置 right总点位置
*/
void quickSort(int arr[],int left,int right)
{
int i = left;
int j = right;
int pivot = arr[(left + right) / ]; // 支点
while(i <= j)
{
while(arr[i] < pivot)
{
i++;
} while(arr[j] > pivot)
{
j--;
} if(i<=j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
} // 递归
if(left < j)
{
quickSort(arr,left,j);
} if(i < right)
{
quickSort(arr,i,right);
}
}
2.冒泡排序
原理:如果当前这个数比下一个数大,则交换位置,经过一次之后最大的数就排到了数组的末尾,以此类推
void bubble(inti arr[],int n)
{
int i ;
int temp;
for(i=;i<n-;i++)
{
if(arr[i] > arr[i + ])
{
temp = arr[i];
arr[i] = arr[i + ];
arr[i + ] = temp;
}
} } void bubbleSort(int arr[],int n)
{
int i;
for(i = n; i>=; i--)
{
bubble(arr,i);
}
}
3.选择排序
思路:
1.首先在数组中找到最大值并记录其下标
2.然后最大值与数组下标为n-1的交换。
int findMaxPos(int arr[],int n)
{
int i;
int max = arr[];
int pos = ;
for(int i = ; i<n; i++)
{
if(arr[i] > max)
{
pos = i;
max = arr[i];
}
}
return pos;
} void selectSort(int arr[],int n)
{
while(n > )
{
int pos = findMaxPos(arr,n);
int temp = arr[pos];
arr[pos] = arr[n - ];
arr[n - ] = temp;
n--;
}
}
4.插入排序
思路:
1.首要条件:两个数及以上
2.取到当前要插入的元素,以前面一个比较,如果小于前面,则需要把前面的大数移位,直到不满足小于的条件,插入相应的位置
void insert(int arr[],int n)
{
int key = arr[n];
int i = n;
while(arr[i - ] > key)
{
arr[i] = arr[i - ];
i--;
if(i == )
{
break;
}
}
arr[i] = key;
} void insertionSort(int arr[],int n)
{
int i;
for(i=; i< n; i++)
{
insert(arr,i);
}
}
5.二分查找插入排序
/*
* 折半查找排序
* 思路:
* 1.直接插入排序中,通过二分查找待插入的位置pos
* 2.将pos + 1 到 i - 1元素往后移动一个位置
* 3.将带排序的元素复制到pos
*/ // 二分查找
/*
* 如查找的数组是: a[4] =[3,2,1,4] maxLength = 4 key对应数组下标的值;
* 第一次查找:pos = 0
* 第二次查找:pos = 1
* 第三次查找:pos = 2
* 第四次查找:
*/
int findPos(int a[],int maxLength,int key)
{
int i = , low =, hight = current - ,pos;
while(low <= hight)
{
pos = (low + hight) / ;
if(a[pos] > key){ // 向左边查找
hight = pos - ;
}else{
low = pos + ;
}else{
return pos; // 返回查找到的位置
} return -; // 没有找到
} // 插入排序
void binInsertSort(int a[])
{
int i = , temp,pos,j;
for(i = ; i<a.count; i++)
{
pos = findPos(a,i,a[i]);
temp = a[i];
for(j = i -;j >= pos;j--)
{
a[j + ] = a[j]; // 元素后移
}
a[pos] = temp;
}
}
6.归并排序
思路:就是将两个已经排好序的数组,合成一个排好序的数组
1.先把数组中单个元素看做是已经排好序的堆
2.合并两个相邻的堆,重复多次,就之后剩下两个在进行合并
int *sort(int a[],int b[])
{
int maxLength = a.length + b.length
int temp[maxLength]; int ai = ;
int bi = ;
int tempi = ;
while(ai < a.length && bi <b.length)
{
if(a[ai] < b[bi])
{
temp[tempi++] = a[ai++];
}
else{
temp[tempi++] = b[bi++];
}
} // 判断那个数组最长,把长出来的继续添加到temp数组中
while(ai < a.length) temp[tempi++] = a[ai++];
while(bi < b.length) temp[tempi++] = b[bi++]; return temp;
}
7.堆排序
思路:
1.先把要排序的数组如a[9]={20,50,20,40,70,10,80,30,60},构造堆(有大顶堆和小顶堆)
在构造有序堆时,我们开始只需要扫描一半的元素(n/2-1 ~ 0)即可,为什么?
因为(n/2-1)~0的节点才有子节点,如图1,n=8,(n/2-1) = 3 即3 2 1 0这个四个节点才有子节点

2.筛选
3.调整堆 private static void heapSort(int[] arr) {
int len = arr.length -1;
for(int i = len/2 - 1; i >=0; i --){ //堆构造
heapAdjust(arr,i,len);
} // 执行到这里,说明根节点(a[0])是最大值或者是最小值了
while (len >=0){
swap(arr,0,len--); //将堆顶元素与尾节点交换后,长度减1,尾元素最大
heapAdjust(arr,0,len); //再次对堆进行调整
}
} public static void heapAdjust(int[] arr,int i,int len){
int left,right,j ;
while((left = 2*i+1) <= len){ //判断当前父节点有无左节点(即有无孩子节点,left为左节点)
right = left + 1; //右节点或者写 2*i+2
j = left; //j"指针指向左节点"
if(j < len && arr[left] < arr[right]) //右节点大于左节点
j ++; //当前把"指针"指向右节点
if(arr[i] < arr[j]) //将父节点与孩子节点交换(如果上面if为真,则arr[j]为右节点,如果为假arr[j]则为左节点)
swap(arr,i,j);
else //说明比孩子节点都大,直接跳出循环语句
break;
i = j;
}
}
public static void swap(int[] arr,int i,int len){
int temp = arr[i];
arr[i] = arr[len];
arr[len] = temp;
}
第四篇、C_快速、冒泡、选择、插入排序、二分查找排序、归并、堆排序的更多相关文章
- 常见的排序算法(直接插入&选择排序&二分查找排序)
1.直接插入排序算法 源码: package com.DiYiZhang;/* 插入排序算法 * 如下进行的是插入,排序算法*/ public class InsertionSort { pub ...
- IOS- 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序
/*******************************快速排序 start**********************************///随即取 当前取第一个,首先找到第一个的位置 ...
- 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)
本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...
- 第四篇 Integration Services:增量加载-Updating Rows
本篇文章是Integration Services系列的第四篇,详细内容请参考原文. 回顾增量加载记住,在SSIS增量加载有三个使用案例:1.New rows-add rows to the dest ...
- 直接插入排序、折半插入排序、Shell排序、冒泡排序,选择排序
一.直接插入排序 稳定,时间复杂度:最好O(n).最差O(n^2).平均O(n^2).空间复杂度O(1) void InsertSort(int L[], int n) { int i, j,key; ...
- 【译】第四篇 Integration Services:增量加载-Updating Rows
本篇文章是Integration Services系列的第四篇,详细内容请参考原文. 回顾增量加载记住,在SSIS增量加载有三个使用案例:1.New rows-add rows to the dest ...
- Spring Cloud第十四篇 | Api网关Zuul
本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...
- [PHP]基本排序(冒泡排序、快速排序、选择排序、插入排序、二分法排序)
冒泡排序: function bubbleSort($array){ $len=count($array); //该层循环控制 需要冒泡的轮数 for($i=1;$i<$len;$i++){ / ...
随机推荐
- python 面向对象高级编程
数据封装.继承和多态只是面向对象程序设计中最基础的3个概念.在Python中,面向对象还有很多高级特性,允许我们写出非常强大的功能. 我们会讨论多重继承.定制类.元类等概念.
- light oj 1008 - Fibsieve`s Fantabulous Birthday
1008 - Fibsieve`s Fantabulous Birthday PDF (English) Statistics Forum Time Limit: 0.5 second(s) Me ...
- XMIND
XMind 是一款非常实用的商业思维导图软件,应用全球最先进的Eclipse RCP 软件架构,全力打造易用.高效的可视化思维软件,强调软件的可扩展.跨平台.稳定性和性能,致力于使用先进的软件技术帮助 ...
- Django中载入js和css文件
Django中载入js和css文件 项目的文件夹结构例如以下: mysite |-mysite |-|-static |-|---js和css文件 |-|-|-init.py |-| |-models ...
- 文件TEXTBOX
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 0703-APP-Notification-statue-bar
1.展示显示textTicker和仅仅有icon的两种情况:当參数showTicker为true时显示否则不显示 // In this sample, we'll use the same text ...
- android学习日记20--连接组件之Intent和IntentFilter
上次刚了解完Android的四大组件,现在学习组件间通信的Intent和IntentFilter 一.Intent 1.简述 Intent(意图)在应用程序运行时连接两个不同组件,是一种运行时的绑定机 ...
- location查询字符串解析
function getQueryStringArgs() { //取得查询字符串并去掉开头的问号 var qs = (location.search.length >0? location.s ...
- PAT 1010
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- 介绍一些实用的IOS手势识别库 (COCOS2D)
http://www.supersuraccoon-cocos2d.com/zh/2012/11/14/introduction-to-some-great-ios-gesture-recogniti ...