插入类排序
1.直接插入排序
排序思想:仅有一个元素的序列总是有序的,因此,对n个记录的序列,可从第二个元素开始直接到第n个元素,逐个向有序序列中执行插入操作,从而得到n个元素按关键字有序的序列。
排序想法:
1.a[0]是一个有序元素,a[0]之后都是待排序元素
2.比较排序与待排序元素大小,若排序元素比待排序元素大,则排序元素的位置往后移动一位,插入排序元素
3.若排序元素比待排序元素小,则在排序元素后插入待排序元素
![](https://img-blog.csdnimg.cn/20190923100909405.png)
package main.yzc.backOffice.test;
public class demo01 {
public void inserSort(int[] a){
int n = a.length;
int i,j;
for (i=1;i<n;i++){
int temp = a[i];
System.out.println("待排序数字"+temp);
//排序数字与待排序数字比较
for (j=i-1;j>=0 && a[j]>temp;j--){
//排序数字比待排序数字大
//排序数字往后移动一位
a[j+1] = a[j];
}
//排序数字比待排序数字小 则直接在排序数字后插入
a[j+1] = temp;
}
printResult(a,n);
}
//排序结果打印
private void printResult(int[] a,int n){
System.out.print("最终排序结果:");
for (int j=0;j<n;j++){
System.out.print(" "+a[j]);
}
System.out.println();
}
public static void main(String[] args) {
int[] a = {5,6,3,9,8,4,2,7};
new demo01().inserSort(a);
}
}
2.折半插入排序
排序思想:直接插入排序的基本操作是向有序列表中插入一个元素,插入位置的确定是通过对有序序列中的元素按关键字逐个比较得到的。既然是在有序序列中确定插入位置,则可以不断二分有序序列来确定插入位置,即搜索插入位置的方法可以使用折半查找实现
package main.yzc.backOffice.test;
public class demo01 {
public void binInsertSort(int[] a){
int n = a.length;
int i,j;
for (i=1;i<n;i++){
//待排序元素
int temp = a[i];
System.out.println("待排序元素:"+temp);
//定义头尾
int low=0;
int high =i-1;
while(low <= high){
//有序数组中间坐标 此时用于二分法查找 减少查找次数
int mid = (low+high)/2;
//若中间元素大于待排序元素 则待排序元素应该排在中间元素之前 需要往前搜索
if (a[mid]>temp){
high = mid - 1;
//若中间元素小于待排序元素 则待排序元素应该排在中间元素之后 需要往后搜索
}else{
low = mid + 1;
}
}
for (j=i-1;j>=low;j--){
//元素后移 为插入temp做准备
a[j+1] = a[j];
}
//将待排序元素插入
a[low] = temp;
}
printResult(a,n);
}
//排序结果打印
private void printResult(int[] a,int n){
System.out.print("最终排序结果:");
for (int j=0;j<n;j++){
System.out.print(" "+a[j]);
}
System.out.println();
}
public static void main(String[] args) {
int[] a = {5,6,3,9,8,4,2,7,10};
new demo01().binInsertSort(a);
}
}
3.希尔排序
排序思想:首先将待排序的元素分为多个子序列,使得每一个子序列的元素个数相对较少,对各个子序列分别进行直接插入排序,待整个待排序序列“基本有序”后,再对所有元素进行一次直接插入排序。
package main.yzc.backOffice.test;
public class demo01 {
public void shellSort(int[] data){
int j = 0;
int temp = 0;
for (int increment = data.length / 2; increment > 0; increment /= 2) {
System.out.println("步长:" + increment);
for (int i = increment; i < data.length; i++) {
temp = data[i];
for (j = i - increment; j >= 0; j -= increment) {
if (temp < data[j]) {
data[j + increment] = data[j];
} else {
break;
}
}
data[j + increment] = temp;
}
printResult(data,data.length);
}
}
//排序结果打印
private void printResult(int[] a,int n){
System.out.print("最终排序结果:");
for (int j=0;j<n;j++){
System.out.print(" "+a[j]);
}
System.out.println();
}
public static void main(String[] args) {
int[] a = {5,6,3,9,8,4,2,7,10};
new demo01().shellSort(a);
}
}
交换类排序
排序思想:首先将n个元素中的第一个和第二个进行比较,如果两个元素的位置为逆序,则交换两个元素的位置;进而比较第二个和第三个元素关键字,如此类推,直到比较第n-1个元素和第n个元素为止。
1.起泡排序
package main.yzc.backOffice.test;
public class demo01 {
public void bubbleSort(int[] arr,int n){
//如果只有一个元素就不用排序了
if (n <= 1) return;
for (int i = 0; i < n; ++i) {
// 提前退出冒泡循环的标志位,即一次比较中没有交换任何元素,这个数组就已经是有序的了
boolean flag = false;
//此处你可能会疑问的j<n-i-1,因为冒泡是把每轮循环中较大的数飘到后面,
for (int j = 0; j < n - i - 1; ++j) {
// 数组下标又是从0开始的,i下标后面已经排序的个数就得多减1,总结就是i增多少,j的循环位置减多少
if (arr[j] > arr[j + 1]) {
//即这两个相邻的数是逆序的,交换
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
if (!flag) break;//没有数据交换,数组已经有序,退出排序
}
printResult(arr,arr.length);
}
//排序结果打印
private void printResult(int[] a,int n){
System.out.print("最终排序结果:");
for (int j=0;j<n;j++){
System.out.print(" "+a[j]);
}
System.out.println();
}
public static void main(String[] args) {
int[] a = {5,10,53,99,45,4,2,7,10};
int n = a.length;
new demo01().bubbleSort(a,n);
}
}
2.快速排序
package main.yzc.backOffice.test;
public class demo01 {
private int partition(int[] a, int low,int high){
int pivot = a[low];
while(low<high){
//从后往前开始比较,直到遇到比pivot小的数,将其移到low位置,此时high位置的数在[]中有两个
while(low<high && a[high]>=pivot) high--;
a[low] = a[high];
//再从前开始比较,当然第一个a[low]是刚移过来的,low++,直到遇到比pivot小的数,将其替换到high位置的值
while(low<high && a[low]<=pivot) low++;
a[high]=a[low];
}
//遍历到后来low>high时,结束
a[low] = pivot;
return low;
}
public void quickSort(int[] data,int low,int high){
if(low<high){
int pivot = partition(data,low,high);
quickSort(data,low,pivot-1);
quickSort(data,pivot+1,high);
}
}
public static void main(String[] args){
demo01 t = new demo01();
int[] a = {3,2,7,10,1,9,0,5};
t.quickSort(a,0,a.length-1);
for(int i : a)
System.out.print(i+" ");
}
}
选择类排序
1.简单的选择排序
package main.yzc.backOffice.test;
public class demo01 {
public static void simpleSelectSort(int[] array) {
if (null == array || array.length <= 1) {
return;
}
for (int i = 0; i < array.length - 1; i++) {
int temp;
int index = i;
for (int j = i + 1; j < array.length; j++) {
if (array[index] > array[j]) {
index = j;
}
}
temp = array[index];
array[index] = array[i];
array[i] = temp;
}
printResult(array);
}
public static void printResult(int[] a){
for (int i : a) {
System.out.print(i+" ");
}
}
public static void main(String[] args) {
int[] a = {1,2,5,7,9,10,15,16,23,28,45};
simpleSelectSort(a);
}
}