java array sorting_java array sorting

Bubble Sort

//Bubble sort: Move the larger value of the two adjacent numbers back one place each time the loop is looped

//Because the outer loop defines the number of loops based on the length of the array, the total number of loops ensures that even if there are two remaining numbers, it will loop once for comparison

//The inner loop ensures that the sorted positions are not compared each time, and the unsorted positions must be compared in a loop even if the conditions are met

//If we want to interrupt this unnecessary operation when the condition is met, we need to judge whether the value in the unsorted position satisfies the condition

//The inner loop traverses are not sorted, the inner loop completes a complete loop without changing the value position,

//Indicates that all the values ​​that are not sorted meet the conditions. At this time, the loop sorting has no meaning, and the loop can be interrupted.

//It can also be sorted in reverse order, moving the smaller backwards

int[] arr = {12,5,54,46,532,2,13,45,12,11};

int num = 0;

boolean bo;

for (int i = 0; i < arr.length-1 ; i++) {

bo = true;

for (int j = 0; j < arr.length-1-i; j++) {

if(arr[j]>arr[j+1]) {

num = arr[j];

arr[j]=arr[j+1];

arr[j+1]=num;

bo = false;

}

}

if(bo) {

System.out.println("i="+i);

break;

}

}

selection sort

//Selection sort: The basic idea of ​​the selection sort algorithm is to select the current smallest element for each position

//First compare the first position with other positions in turn, put the smallest first,

//It can also be sorted in reverse order, putting the larger one first

//Method 1: Swap the smaller value to the front each time

int[] arr = {12,5,54,46,532,2,13,45,12,11};

for(int i = 0 ; i < arr.length-1 ; i++){

for(int j = i+1 ; j < arr.length ; j++){

if(arr[i] > arr[j]){

int num = arr[i];

arr[i] = arr[j];

arr[j] = num;

}

}

}

//方法二:一次性找到最小值的下标,完成内循环后将第一位与最小值交换

for (int i = 0; i < arr.length-1; i++) {

int index = i;

for (int j = i+1; j < arr.length; j++) {

if(arr[index]>arr[j]) {

index=j;

}

}

if(index!=i) {

int num = arr[index];

arr[index]=arr[i];

arr[i]=num;

}

}

快速排序

//快速排序:根据一个定量将比该值大的放在右边,比该值小的放在左边,分成左右两部分,通过递归依次分组实现排序

//1.将数组第一个元素作为标记值,左右两边通过下标获取值进行比较

//2.左边值小于标记值,则将左边下标后移继续比较,直到相同或大于比较值,下标不动,以待将该值移动到右侧

//3.右边值大于标记值,则将右边下标前移继续比较,直到相同或小于比较值,下标不动,以待将该值移动到左侧

//4.完成上述步骤后,需要移动两边数据,(先判断一下两个数据是否相同,相同则不移动,同时移动一个下标)

//5.当两边下标移动到一块时,说明数组已经完成了分组,分组边界为两下标所在处

//6.然后分别让两组数据通过递归方式继续进行上述步骤,直到分组后数组元素唯一(即递归出口)

public static void main(String[] args){

int[] arr = {1,2,12,3,132,1,52,5,4,65,4,685,321};

qsort(arr,0,arr.length-1);

}

public static void qsort(int arr[],int start,int end) {

int number = arr[start];

int i = start;

int j = end;

while(i

while(inumber) {

j--;

}

while(i

i++;

}

if(arr[i]==arr[j]&&i

i++;

}

else {

int num = arr[j];

arr[j] = arr[i];

arr[i] = num;

}

}

if(i-1>start) {

qsort(arr,start,i-1);

}

if(j+1

qsort(arr,j+1,end);

}

}

插入排序

//插入排序:数组左侧为排好序的数值,右侧依次遍历时候,将数值放进左侧数组中合适的位置

//1.将需要排序的数值与下标拿出来,然后用这个数值与前边排好序的数依次比较

//2.如果前边的数大,将前边的数向后移动一位,并将空出来的下标预留给新数值,直到前边的数不大于新数值

//3.将新数值放到空出来的位置上,然后继续往后拿下一个新数值

for (int i = 1; i < arr.length; i++) {

int number = arr[i];

int index = i;

for (int j = i-1; j >=0; j--) {

if(number

arr[j+1]=arr[j];

index = j;

}

}

arr[index]=number;

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324187104&siteId=291194637