Insert Sort
publicstaticvoidinsertSort(int[] arr){ for(int0 ; i < arr.length ; i ++){ int tmp = arr[i]; int0; for1; (j > -1) && (tmp < arr[j]) ; j --){ 1] = arr[j]; } 1] = tmp; }
Bubble sort
Public Static void Bubblesort(int[] arr) {intLength = Arr.length-1;intTMP =0;BooleanFlag =false; while(Length! =0) {flag =false; for(inti =0; i < length; i + +) {if(Arr[i] > arr[i+1]) {tmp = Arr[i]; Arr[i] = arr[i+1]; arr[i+1] = tmp; Flag =true; }} System.out.println ("section"+ (Arr.length-length) +"Trip:"+arrays.tostring (arr)); length--;if(Flag = =false){//There is no data exchange action in this process, indicating that the array is already in order Break; } } }
Select sort
(1):将序列分成有序区和无序区,初始时有序区为空,无序区包括全部元素(2):每次从无序区中选择最小的元素将其与无序区第一个元素进行交换(3):重复(2),直到无序区没有元素
Public Static void Selectsort(int[] arr) {intTMP =0;intindex =0; for(inti =0; i < arr.length; i + +) {index = i;//Find the minimum value in [I...length] for(intj = i +1; J < Arr.length; J + +) {if(Arr[index] > Arr[j]) {index = j; } }//Minimum element is not itself if(Index! = i) {tmp = Arr[index]; Arr[index] = Arr[i]; Arr[i] = tmp; } } }
# #归并排序
Public Static void MergeSort(int[] list) {if(List.length >1){//Merge the left half part int[] Lefthalf =New int[list.length/2]; System.arraycopy (list,0, Lefthalf,0, list.length/2); MergeSort (lefthalf);//Merge right Half part intRighthalflength = list.length-list.length/2;int[] Righthalf =New int[Righthalflength]; System.arraycopy (List,list.length/2, Righthalf,0, righthalflength); MergeSort (righthalf);int[] temp = merge (lefthalf,righthalf); System.arraycopy (temp,0, List,0, temp.length); } } Public Static int[]Merge(int[] List1,int[] list2) {int[] temp =New int[List1.length + list2.length];intPOS1 =0;//list1 Cursors intPos2 =0;//list2 Cursors intPOS3 =0;//temp Cursors while(Pos1 < List1.length && Pos2 < list2.length) {if(LIST1[POS1] < List2[pos2]) {temp[pos3++] = list1[pos1++]; }Else if(LIST1[POS1] > List2[pos2]) {temp[pos3++] = list2[pos2++]; }Else{temp[pos3++] = list2[pos2++]; temp[pos3++] = list1[pos1++]; } } while(Pos1 < List1.length) {temp[pos3++] = list1[pos1++]; } while(Pos2 < List2.length) {temp[pos3++] = list2[pos2++]; }returnTemp }
Quick Sort
Public Static void QuickSort(int[] arr,intLowintHigh) {if(High > Low) {intPart = partition (Arr,low,high);//system.out.println ("part=" +part);QuickSort (Arr,low,part-1); QuickSort (arr,part+1, high); } }//Find the demarcation point Public Static int Partition(int[] arr,intLow,intHigh) {intfirst = low;intPivot = Arr[low]; low++; while(High > Low) {//Find a number larger than pivot from the left while(Low <= && Arr[low] <= pivot) {low++; }//From the right to find a smaller or equal number than pivot while(High >= low && Arr[high] > Pivot) {high--; }//Exchange if(High > Low) {inttemp = Arr[high]; Arr[high] = Arr[low]; Arr[low] = temp; } }//At this time high = low-1; //And elements before high are less than or equal to Pivot,low after the element is greater than pivot //system.out.println ("low=" +low+ "; high=" +high+ ":" +arrays.tostring (arr)); if(High > First) {inttemp = Arr[high]; Arr[high] = Arr[first]; Arr[first] = temp;//system.out.println ("part=" +high+ ":" +arrays.tostring (arr)); returnHigh }Else{//system.out.println ("part=" +high+ ":" +arrays.tostring (arr)); returnFirst } }
Heap Sort
堆是一棵具有以下属性的二叉树 1、一棵完全二叉树 2、每个节点大于(小于)或等于它的任意一个孩子 分类:大顶堆,小顶堆
Public Static void Heapsort(intArr[]) {//Initialize heapInitheap (arr);//system.out.println ("Initialize heap:" + arrays.tostring (arr)); //end represents the end position of the heap intEnd = Arr.length-1; while(End >0){//delete heap top element inttemp = arr[0]; arr[0] = Arr[end]; Arr[end] = temp; end--;if(End >0){//Adjustment heapAdjustheap (Arr,end); } } } Public Static void Initheap(int[] arr) {//From 0 count, then node of position I, left child at position 2i+1, right child at position 2i+2, parent node at (i-1)/2 for(inti =1; i < arr.length; i + +) {intcurrent = i; while(Current >0){intParent = (Current-1) /2;//child node is larger than parent node, swap location if(Arr[current] > Arr[parent]) {inttemp = arr[current]; Arr[current] = arr[parent]; Arr[parent] = temp;//upward adjustment, with new currentcurrent = parent; }Else{ Break; } } } } Public Static void Adjustheap(int[] arr,intEnd) {//Remove top of heap element, adjust the location of the heap top //need to know that because deleting a heap top only adjusts the value of the element at the top of the heap, //So after the top of the heap is removed, the left and right are the trees that match the heap When the top element of the heap is detected to conform to the definition of the heap, //And since its left and right subtree is always the definition of a heap, the adjustment process can end intCurrent =0; while(Current < end) {intleft = current *2+1;intright = current *2+2;if(Left <= end && right <= end) {///left and right nodes are present if(! (Arr[current] >= Arr[left] && arr[current] >= arr[right])) {//need to adjust the heap top intindex = left;if(Arr[left] < arr[right]) {//Left node less than right nodeindex = right; }inttemp = arr[current]; Arr[current] = Arr[index]; Arr[index] = temp; current = index; }Else{//This is already a heap, no need to continue downward adjustment Break; } }Else if(Left <= end && right > End) {//Left node exists but right node does not exist if(Arr[current] < Arr[left]) {inttemp = arr[current]; Arr[current] = Arr[left]; Arr[left] = temp; current = left; }Else{//This is already a heap, no need to continue downward adjustment Break; } }Else{///left and right nodes do not exist, have been adjusted to the end Break; } } }
Java implements a variety of sorting algorithms