排序算法一(冒泡排序、選擇排序、插入排序)


作者:xcbeyond
瘋狂源自夢(mèng)想,技術(shù)成就輝煌!微信公眾號(hào):《程序猿技術(shù)大咖》號(hào)主,專注后端開發(fā)多年,擁有豐富的研發(fā)經(jīng)驗(yàn),樂于技術(shù)輸出、分享,現(xiàn)階段從事微服務(wù)架構(gòu)項(xiàng)目的研發(fā)工作,涉及架構(gòu)設(shè)計(jì)、技術(shù)選型、業(yè)務(wù)研發(fā)等工作。對(duì)于Java、微服務(wù)、數(shù)據(jù)庫(kù)、Docker有深入了解,并有大量的調(diào)優(yōu)經(jīng)驗(yàn)。 






   



一、冒泡排序:

1、算法思想:

    對(duì)要排序的數(shù)據(jù),從上到下依次比較兩個(gè)相鄰的數(shù)并加以調(diào)整,將最大的數(shù)向下移動(dòng),較小的數(shù)向上冒起。即:每一趟依次比較相鄰的兩個(gè)數(shù)據(jù)元素,將較小的數(shù)放在左邊,循環(huán)進(jìn)行同樣的操作,直到全部待排序的數(shù)據(jù)元素排完。

2、實(shí)例分析:

   例如:我們要將身高不等的十個(gè)人站在一排,要求他們按照身高由低到高排隊(duì),設(shè)將10個(gè)人編號(hào)為0---9 ,相鄰的兩個(gè)人依次比較,如果左邊的比右邊的人高,則交換兩個(gè)人的位置,否則不交換,直到最后兩個(gè)人,即此時(shí)完成了一趟排序。一趟排序后,最高的人已經(jīng)在最右邊了。

在這里插入圖片描述

一趟排序后的結(jié)果:(將最高者一趟排序后移動(dòng)到最后位置)

如此都多趟的排序,最終就完成了整個(gè)的排序。

3、算法分析:(有小到大排序)

  1>、每一趟過程中,就是依次比較兩個(gè)相鄰的數(shù),若a[i]>a[i+1],則交換兩數(shù),否則不換;

   2>、每一趟就是一重循環(huán),而由于要經(jīng)過多趟過程,即外面還有一重循環(huán),所以就存在雙重循環(huán)。

4、算法代碼:(Java版)

/**
 * 冒泡排序 算法
 * @author xcbeyond
 *
 */
 
public class BubbleSort {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] ={13,15,37,89,60,39,12,109,56,72} ;
		int i = 0;
		int j = 0;
		
		for(i=0;i<10;i++)
			System.out.print(a[i]+"  ");
		System.out.println();
		
		for(i=0;i<a.length;i++)
			for(j=0;j<a.length-i-1;j++)
			{
				if(a[j]>a[j+1])
				{
					int temp;
					temp=a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
			}
		
		for(i=0;i<10;i++)
			System.out.print(a[i]+"  ");
		
	}
 
}

二、選擇排序

1、算法思想:

    將待排序序列分為兩部分,一部分為有序序列,另一部分為無序序列。第一趟:從a[0]到a[n-1]中找到最小的數(shù)a[i],然后將a[i]與a[0]交換,第二趟:從a[1]到a[n-1]中找到最小的數(shù)a[j],然后將a[j]與a[1]交換,第三趟:從a[2]到a[n-1]中找到最小的數(shù)a[k],然后將a[k]與a[2]交換 ……

2、實(shí)例分析:

  {13,15,37,89,60,39,12,109,56,72}

第一趟 :12 {15,37,89,60,39,13,109,56,72}

第二趟:12 ,13 {37,89,60,39,15,109,56,72}

第三趟:12 ,13 ,15 {89,60,39,37,109,56,72}

……






3、算法代碼:

/**
 * 選擇排序
 * @author xcbeyond
 *
 */
public class SelectSort {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] ={13,15,37,89,60,39,12,109,56,72} ;
		int i;
		int j;
		
		for(i = 0;i<a.length;i++)
			System.out.print(a[i]+"  ");
		System.out.println();
		
		for(i = 0;i<a.length;i++){
			int min = a[i];
			for(j = i+1;j<a.length;j++){
				if(min>a[j]){
					int temp;
					temp = min;
					min = a[j];
					a[j] = temp;
				}
			}
			a[i] = min;
		}
		
		for(i = 0;i<a.length;i++)
			System.out.print(a[i]+"  ");
	}
 
}

三、插入排序

1、算法思想:

1〉從第一個(gè)元素開始,該元素可以認(rèn)為已經(jīng)被排序

2〉取第一個(gè)未排序元素存放在臨時(shí)變量temp中,在已經(jīng)排序的元素序列中從后往前掃描,逐一比較

3〉如果temp小于已排序元素,將該元素移到下個(gè)位置

4〉重復(fù)步驟3〉,直到找到已排序的元素小于或者等于

2、實(shí)例分析:

   {13,15,37,89,60,39,12,109,56,72}

第一趟: 13 {15,37,89,60,39,12,109,56,72} temp = 15

                 temp>13

第二趟: 13 ,15 { 37,89,60,39,12,109,56,72} temp = 37

                temp>15         temp>13 

第三趟: 13,15 ,37 {89,60,39,12,109,56,72} temp = 89

               temp>37       temp>15       temp>13

第三趟: 13,15 ,37 ,89 {60,39,12,109,56,72} temp =60

               temp< 89:60<->89          13,15 ,37 , 60,89 {39,12,109,56,72}  

                temp>37        temp>15        temp>13

第四趟: 13,15 ,37 , 60,89 {39,12,109,56,72} temp = 39

               temp<89:  39<->89           13,15 ,37 , 60,39,89 {12,109,56,72}   

              temp<60  :   39<->60           13,15 ,37 , 39,60,89 {12,109,56,72}   

               temp>37      temp>15         temp >13

第五趟: 13,15 ,37 , 39,60,89 {12,109,56,72} temp = 12

                temp<89 :12<->89                   13,15 ,37 ,39,60, 12,89 {109,56,72}  

                temp<60:12<->60                    13,15 ,37 ,39,12,60,89 {109,56,72}  

                temp<39 :12<->39                   13,15 ,37 ,12,39,60,89 {109,56,72}  

               temp<37 :12<->37                    13,15 ,12 ,37 ,39,60,89 {109,56,72}  

               temp<15: 12<->15                   13,12,15 ,37 ,39,60,89 {109,56,72}  

               temp<13 :12<->13                     12,13,15 ,37 ,39,60,89 {109,56,72}  

第六趟: 12,13,15 ,37 ,39,60,89 {109,56,72} temp = 109

……

3、算法代碼:

import java.util.Arrays;
 
/**
 * 插入排序
 * @author xcbeyond
 *
 */
public class InsertSort {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] ={13,15,37,89,60,39,12,109,56,72} ;
		a = insertSort(a);
		String s = Arrays.toString(a);
		System.out.println(s);
	}
	
	public static int[] insertSort(int[] ary){
		for(int i = 1;i < ary.length;i++){
			int temp = ary[i];
			int j;
			for(j = i-1;j>=0 && temp < ary[j];j--){
				ary[j+1] = ary[j];
			}
			ary[j+1] = temp;
		}
		
		return ary;
	}
}

另一種(易于理解):

import java.util.Arrays;
 
/**
 * 插入排序
 * @author xcbeyond
 *
 */
public class InsertSort {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] ={13,15,37,89,60,39,12,109,56,72} ;
		a = insertSort(a);
		String s = Arrays.toString(a);
		System.out.println(s);
	}
	
	public static int[] insertSort(int[] ary){
		for(int i = 1;i < ary.length;i++){
			int temp = ary[i];
			int j;
			for(j = i-1;j>=0;j--){
				if(temp < ary[j]){
					ary[j+1] = ary[j];
				}
				else
					break; //找到插入位置
			}
			ary[j+1] = temp;
		}
		
		return ary;
	}
}