话不多说,直接开始
1.选择排序
原理:遍历数组选择符合条件的数进行互换。对这个某个数组进行排序,首先定住第一个数,从第二个数开始往后找,找到一个比第一个数小的数进行互换,整个数组遍历完之后,定住第二个数,从第三个数字开始往后找...以此类推。
👇--------------👇
例如: [ 3, 2, 5, 9, 4, 1, 10, 6]
第二层循环结束1次后 [ 1, 2, 5, 9, 4, 3, 10, 6]
👇-------👇
第二层循环结束2次后 [ 1, 2, 5, 9, 4, 3, 10, 6]
👇👇
第二层循环结束3次后 [ 1, 2, 3, 9, 4, 5, 10, 6]
👇👇
第二层循环结束4次后 [ 1, 2, 3, 4, 9, 5, 10, 6]
👇-----👇
第二层循环结束5次后 [ 1, 2, 3, 4, 5, 9, 10, 6]
👇-👇
第二层循环结束6次后 [ 1, 2, 3, 4, 5, 6, 10, 9]
第二层循环结束7次后 [ 1, 2, 3, 4, 5, 6, 9, 10]
代码实现
Java版
public class Demo{
public static void main(String[] args){
int[] array = {3, 2, 5, 9, 4, 1, 10, 6};
int temp, mark;
// 快捷版
for(int i=0; i < array.length-1; i++){
mark = i;
for(int j = i+1; j < array.length; j++){
if(array[j] < array[mark]){
mark = j;
}
}
if(mark != i){
temp = array[i];
array[i] = array[mark];
array[mark] = temp;
}
}
/*
蠢一点的方法:虽然代码少了,但是速度相对慢了
for(int i=0; i < array.length-1; i++) {
for(int j=i+1; j < array.length; j++) {
if(array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
*/
}
}
C语言版
#include<stdio.h>
#include<stdlib.h>
int main()
{
int array[] = {3, 2, 5, 9, 4, 1, 10, 6};
int temp, mark;
for(int i=0; i<7; i++)
{
mark = i;
for(int j=0; j<8; j++)
{
if(array[mark] > array[j]) mark = j;
}
if(mark != i)
{
temp = array[mark];
array[mark] = array[i];
array[i] = temp;
}
}
return 0;
}
2.冒泡排序
原理:遍历数组的过程中,比较本元素和下一个元素的大小,如果大于下一个元素,则与下一个元素互换,否则不进行处理。这样讲可能比较抽象,换一个形象一点的例子:不知道你们幼儿园或者小学还是什么时候有没有试过就是,当老师说,高的排后面,矮的排前面的时候,每个人如果看到自己比前面的人矮的时候,就会和前面的人互换位置,经历数次之后便能达到效果,这就是冒泡排序的基本原理。
例如:
第一次冒泡
[ 3, 2, 5, 9, 4, 1, 10, 6]
👆-👆
[ 2, 3, 5, 9, 4, 1, 10, 6]
👆-👆
[ 2, 3, 5, 4, 9, 1, 10, 6]
👆-👆
[ 2, 3, 5, 4, 1, 9, 10, 6]
👆-👆
[ 2, 3, 5, 4, 1, 9, 6, 10]
第二次冒泡
↓--↓
[ 2, 3, 5, 4, 1, 9, 6, 10]
↓--↓
[ 2, 3, 4, 5, 1, 9, 6, 10]
↓--↓
[ 2, 3, 4, 1, 5, 9, 6, 10]
[ 2, 3, 4, 1, 5, 6, 9, 10]
第三次冒泡
[ 2, 3, 4, 1, 5, 6, 9, 10]
↑--↑
[ 2, 3, 1, 4, 5, 6, 9, 10]
第四次冒泡
[ 2, 3, 1, 4, 5, 6, 9, 10]
↑--↑
[ 2, 1, 3, 4, 5, 6, 9, 10]
第五次冒泡
[ 2, 1, 3, 4, 5, 6, 9, 10]
↑--↑
[ 1, 2, 3, 4, 5, 6, 9, 10]
代码实现
Java版:这是小编接触冒泡排序时,书上的示例,但是觉得这个好像不太好,所以有了下面的版本
public class Demo{
public static void main(String[] args){
int[] array = { 3, 2, 5, 9, 4, 1, 10, 6};
hubbleSort(array);
}
public static void hubbleSort(int[] array) {
int len = array.length;
int temp;
for(int i=0; i<len; i++) {
for(int j=0; j<len-1; j++) {
if(array[j] > array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
}
Java的小编改版
public class Demo{
public static void main(String[] args){
int[] array = { 3, 2, 5, 9, 4, 1, 10, 6};
my_hubbleSort(array);
}
public static void my_hubbleSort(int[] array) {
int len = array.length;
int C = 1;
int temp;
while(C != 0) {
C = 0;
for(int i=0; i < len-1; i++) {
if(array[i+1] < array[i]) {
temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
C++;
}
}
}
}
}
C语言版#1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int array[] = {3, 2, 5, 9, 4, 1, 10, 6};
hubbleSort(array, 8);
return 0;
}
void hubbleSort(int array[], int len)
{
int temp;
for(int i=0; i<len; i++)
{
for(int j=0; j<len-1; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
C语言版#2
#include<stdio.h>
#include<stdlib.h>
void my_hubbleSort(int array[], int len);
int main()
{
int array[] = {3, 2, 5, 9, 4, 1, 10, 6};
my_hubbleSort(array, 8);
return 0;
}
void my_hubbleSort(int array[], int len)
{
int C = 1;
int temp;
while(C != 0)
{
C = 0;
for(int i=0; i < len-1; i++)
{
if(array[i+1] < array[i])
{
temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
C++;
}
}
}
}
3.快速排序
运行原理:
1.1把数组的第一个数作为基准key
1.2定义两个标记,head=0和tail=数组的长度-1
2.1在数组的尾部往前,寻找比key小的数并赋值给数组的第head个数,往前找的同时tail也往前移动( tail--),而且head < tail ,找到后跳出循环
2.2在数组的头部往后,寻找比key大的数并赋值给数组的第tail个数,往前找的同时head也往后移动( head++ ),而且head < tail ,找到后跳出循环
3 把2.1和2.2放入while(head < tail){} 的循环中,最后跳出循环时令 数组的第head个数 = 基准key,并return head,把这个功能封装在函数selectStandard(int[] array, int head, int tail){}中
4. 定义quickSort() 递归调用 selectStandard函数
quickSort(int[] array, int head, int tail){
if(head < tail){
int standard = return head的函数;
quickSort(array, head, standard-1);
quickSort(array, standard+1, tail);
}
}
例如
head tail
↓ ↓
[ 3, 2, 5, 9, 4, 1, 10, 6]
↑
key
第一次 key = 3
1. ( 1 比 3 小
head tail
↓ ↓
[ 3, 2, 5, 9, 4, 1, 10, 6]
-------------------------------------------------------
head tail
↓ ↓
[ 1, 2, 5, 9, 4, 1, 10, 6]
-------------------------------------------------------
2. ( 5 比 3 大
head tail
↓ ↓
[ 1, 2, 5, 9, 4, 1, 10, 6]
------------------------------------------------------
head tail
↓ ↓
[ 1, 2, 5, 9, 4, 5, 10, 6]
------------------------------------------------------
3.
head tail
↓ ↓
[ 1, 2, 5, 9, 4, 5, 10, 6]
------------------------------------------------------
未找到比3小的数,最终
[ 1, 2, 3, 9, 4, 5, 10, 6]
并返回2
第二次的1,数组0~1正序无需排序
第二次的2,数组3~7依旧乱序
head tail
↓ ↓
[ 1, 2, 3, 9, 4, 5, 10, 6]
↑
key
key = 9
1. ( 6 比 9 小)
head tail
↓ ↓
[ 1, 2, 3, 9, 4, 5, 10, 6]
---------------------------------------------------
head tail
↓ ↓
[ 1, 2, 3, 6, 4, 5, 10, 6]
--------------------------------------------------
2. ( 10 比 9 大
head tail
↓ ↓
[ 1, 2, 3, 6, 4, 5, 10, 6]
------------------------------------------------
head tail
↓ ↓
[ 1, 2, 3, 6, 4, 5, 10, 10]
-------------------------------------------------
3. 无比9小的数,最终
[ 1, 2, 3, 6, 4, 5, 9, 10]
并return 6
第三次的1右边正序
第三次的2左边乱序
head tail
↓ ↓
[ 1, 2, 3, 6, 4, 5, 9, 10]
↑
key
key = 6
1. 5 比6小
head tail
↓ ↓
[ 1, 2, 3, 6, 4, 5, 9, 10]
----------------------------------------------
head tail
↓ ↓
[ 1, 2, 3, 5, 4, 5, 9, 10]
----------------------------------------------
2. 未找到比6大的数,结果
[ 1, 2, 3, 5, 4, 6, 9, 10]
并return5
第四次
head tail
↓ ↓
[ 1, 2, 3, 5, 4, 6, 9, 10]
同理。。。。。
结果
[ 1, 2, 3, 4, 5, 6, 9, 10]
代码实现
Java版
public class Demo{
public static void main(String[] args){
int[] array = { 3, 2, 5, 9, 4, 1, 10, 6};
quickSort(array, 0, array.length-1);
}
public static int selectStandard(int[] array, int head, int tail) {
int key = array[head];
while(head < tail) {
while(head < tail) {
if(array[tail] < key) {
array[head] = array[tail];
break;
}
tail--;
}
while(head < tail) {
if(array[head] > key) {
array[tail] = array[head];
break;
}
head++;
}
}
array[head] = key;
return head;
}
public static void quickSort(int[] array, int head, int tail) {
if(head < tail) {
int standard = selectStandard(array, head, tail);
quickSort(array, head, standard-1);
quickSort(array, standard+1, tail);
}
}
}
C语言版
#include <stdio.h>
#include <stdlib.h>
void quickSort(int array[], int head, int tail);
int selectStandard(int array[], int head, int tail);
int main()
{
int array[] = { 3, 2, 5, 9, 4, 1, 10, 6};
quickSort(array, 0, 7);
return 0;
}
int selectStandard(int array[], int head, int tail)
{
int key = array[head];
while(head < tail)
{
while(head < tail)
{
if(array[tail] < key)
{
array[head] = array[tail];
break;
}
tail--;
}
while(head < tail)
{
if(array[head] > key)
{
array[tail] = array[head];
break;
}
head++;
}
}
array[head] = key;
return head;
}
void quickSort(int array[], int head, int tail)
{
if(head < tail)
{
int standard = selectStandard(array, head, tail);
quickSort(array, head, standard-1);
quickSort(array, standard+1, tail);
}
}
快速排序总结:其实小编只知道快速排序的运行原理,但是并不知道快速排序为啥快。知其然也知其所以然,但是不知其所以然的所以然。
小编还有许多需要改进的地方,望各路高手指出并进行指导。