[Java] nine and implement sorting algorithms summary

1. Direct insertion sort

   Thought: each pass will be a row as a key element, according to the size thereof is inserted into the key has been sorted partial sequence place on until insertion is completed.

   achieve:

     static void insertSort public (ARR int []) { 
		for (int I =. 1; I <arr.length; I ++) {// begin from the second interpolated value 
			int value = arr [i]; // element to be inserted 
			POST = I int; 
			the while (POST> 0 && ARR [-POST. 1]> value) { 
				ARR [POST] = ARR [-POST. 1]; 
				POST -; 
			} 
			ARR [POST] = value; // find inserted position 
		} 
	}

 

 
2. Bubble Sort

   Idea: a series of actions to complete the exchange.

    First pass: a first element with the second comparison, Ruoguo former than the latter, the two switching positions, a pairwise comparison, up to a maximum position to the final element.

    Second pass: a first comparison with the second element, has been compared to n-1

   achieve:

Fundamentals bubble sort // 
	public static void bubbleSort1 (int [] ARR, int n) { 
		int I, J; 
		for (I = n-. 1; I> 0; I -) {// outer loop comparator n -1 times: first determining a value on the last position of the last determined value to the second position 
			for (j = 0; j < i; j ++) {// inner loop 
				if (arr [j] > ARR [J +. 1]) { 
					int ARR TEMP = [J +. 1]; 
					ARR [J +. 1] = ARR [J]; 
					ARR [J] = TEMP; 
				} 
			} 
		} 
	}
 
  // Version improved bubble sort 
	public static void bubbleSort2 (int [] ARR, n-int) { 
		int I, J; 
		for (I = n--. 1; I> 0; I -) { 
			int In Flag = 0; 
			for ( J = 0; J <I; J ++) { 
				IF (ARR [J]> ARR [J +. 1]) { 
					int TEMP = ARR [J +. 1]; 
					ARR [J +. 1] = ARR [J]; 
					ARR [ J] = TEMP; 
					In flag =. 1; // if exchange occurs, then let labeled. 1 
				  } 
				} 
		      IF (In flag == 0)     
		       {return;}   
		} 
	}

3. Select Sort

thought:

    Select is also a sort exchange sort, and bubble sort has a certain similarity can be considered an improvement bubbling.
    1. unsorted sequence found in the maximum or minimum value, the collating sequence into the starting position
    2. continue looking for the maximum (smaller) element from the remaining elements of unsorted, sorted into the end of the column.
    Step 3. Repeat until all the elements are sorted.

achieve:

SelectSort class {public 
        // increment sequence so 
	public static void SelectSort (int [] ARR, n-int) {   
		int TEMP, min = 0; 
		IF ((ARR == null) || (arr.length == 0)) 
	        return ; 
	    for (int I = 0; I <n-; I ++) { 
	        min = I; // minimum data array subscript disordered region 
	                            // Find the minimum cycle 
	        for (int j = i + 1 ; j <n; j ++ ) {// find the minimum data stored in the random region and its array index 
	            IF (ARR [min]> ARR [J]) { 
	                min = J; 
	            } 
	        } 
	        ! IF (min = I) { 
	            TEMP ARR = [I ]; 
	            ARR [I] = ARR [min]; 
	            ARR [min] = TEMP; 
	        } 
	    } 
	}	 
}
 

4. Quick Sort

 thought:

     1. Define two pointers point to the first element and the last element of the array.

     2. In the first pass the first reference element, from the forward scan, until it encounters a smaller than the reference value, exchange value of the two pointers; transducer scanning direction from the front backwards, until it encounters greater than the reference value, exchange value of the two pointers.
     When the head and tail pointers two coincide, a pointer to a reference value; its value and the reference points to be interchanged.

     3. The front and rear portions, respectively, of the recursive fast-discharge.

 achieve:

public class QuickSort{
	public static void quickSort3(int[] arr,int low,int high){
		if (low>high){
		   return;
		}
		int i,j,temp;
		i=low;
		j=high;
		temp=arr[low];
		while (i!=j){
			while(i<j&&temp<=arr[j]) {j--;}
			  if (i<j){
			  arr[i]=arr[j];
			   i++;
			  }
			
		    while (i<j&&temp>=arr[i]){i++;}
			if(i<j){
			 arr[j]=arr[i];
			 j--;
			}
		 }
		arr[i]=temp;
		quickSort(arr,low,i-1);
		quickSort(arr,i+1,high);
	}
}

 5. merge sort

 Thought: The two ordered arrays into an ordered array; divide and conquer, the recursive array into an array of length 1, and then combined into an ordered array.

 achieve:

public class Merge_Sort { 
	public static void Sort (int [] A, int left, int right) { 
        int MID = (left + right) / 2;   
        IF (left <right) { 
            Sort (A, left, MID); 
            Sort ( a, mid + 1, right) ; 
            about // merge 
            merge (a, left, MID, right); 
        } 
    } 
	// put into one of two ordered ordered 
	public static void merge (int [] arr, L int, int m, R & lt int) { 
		int [] int new new TEMP = [L-R & lt +. 1]; 
		int I = L; 
		int m = J +. 1; 
		int K = 0; 
		// put the two smaller added to the new array 
		the while (I <= m && J <= R & lt) { 
			IF (ARR [I] <ARR [J]) 
				TEMP [K ++] = ARR [I ++]; 
			the else 
				TEMP [K ++] = ARR [J ++];  
		}
		// add the number to the left of the rest of the new array
		the while (I <= m) { 
			TEMP [K ++] = ARR [I ++]; 
		} 
		// add the number of remaining right side of the array into 
		the while (J <= R & lt) { 
			TEMP [K ++] = ARR [J ++]; 
		} 
		// the number assigned to the new array in the original array 
		for (int X = 0; X <temp.length; X ++) { 
			ARR [L + X] = TEMP [X]; 
			//System.out.print(arr [L + X] + ""); 
		}     
	} 
}

6. heapsort

:( great idea to top the heap, for example)

Heap adjustment (heapify) : maximum value of the parent node, Ruoguo not, exchange, and recursive node after exchange adjustments. Top of the heap element guarantee maximum.

Built heap : make an upward adjustment from the last non-leaf node.

Heap (descending order) : the root of the big top stack and the last switching node, the last node to ensure a maximum position. Because the exchange has not done a heap, so the need to do it again heapify from the root node.

The second pass, each node the current node and the current position of the last exchange, ..... sequentially

achieve:

Heap_Big class {public 
	// byte and establish itself on the left and right points of each top node of a stack of large 
	public static void heapify (int [] Tree, n-int, int I) { 
		IF (I> = n-) 
			return; 
		int C1 = 2 * I +. 1; 
		int C2 = 2 * I + 2; 
		int max = I; 
		IF (C1 <n-&& Tree [max] <Tree [C1]) 
			max = C1;	 
		IF (C2 <n-&& Tree [max ] <Tree [C2]) 
			max = C2; 
		IF (max = I) {! 
			the swap (Tree, max, I); 
			heapify (Tree, n-, max); // recursive establishing a top pair after the large switching node End heap 
		} 
	} 

	Private static void the swap (int [] Tree, I int, int J) { 
		int Tree TEMP = [I]; 
		Tree [I] = Tree [J]; 
		Tree [J] = TEMP; 
	} 
	
	// of a the establishment of a large number of group heap top 
	public static void build_heap (int [] tree, int n ) { 
		int. 1-n-last_node =; 
		int parent = (-last_node. 1) / 2; 
		for (int I = parent; I> = 0; I -) { 
			heapify (Tree, n-, I); 
		} 
	} 
	
	// hEAPSORT: the big heap of root and top the last switching node, the last node to ensure a maximum position, 
	// since the exchange is not a complete stack, so from the root node again we need to make a second pass heapify, the switching nodes and each node on the current position of a last current,. 
	public static void heap_sort (int [] Tree, n-int) { 
		build_heap (Tree, n-); 
		for (int =. 1-n-I; I> = 0; I -) { 
			the swap (Tree, I, 0); 
			heapify (Tree, I, 0); 
		} 
	} 
        public static void main (String [] args) { 
		int [] } = {4,10,5,3,1,2 Array; 
		int = n-be array.length; 
		heapify (Array, n-, 0); 
		System.out.println ( "heapify process:"); 
		for (int I = 0; I <n-; I ++) { 
			of System.out.print (Array [I] + ""); 
		}

		System.out.println();
		build_heap(array,n);
		System.out.println("建堆的过程:");
		for (int i=0;i<n;i++){
			System.out.print(array[i]+" ");
		}
		
		System.out.println();
		heap_sort(array,n);
		System.out.println("堆排序的结果");
		for (int i=0;i<n;i++){
			System.out.print(array[i]+" ");
		}
	}
}

 

//小顶堆
public class Heap_Small {
//小顶堆调整的过程 public static void heapify2(int[] tree,int n, int i){ if(i>=n) return; int c1=2 * i +1; int c2=2*i+2; int min=i; if (c1<n && tree[c1]<tree[min]) min=c1; if (c2<n && tree[c2]<tree[min]) min=c2; if (min != i){ swap(tree,min,i); heapify2(tree,n,min); } } private static void swap(int[] tree, int i, int j) { int temp=tree[i]; tree[i]=tree[j]; tree[j]=temp; }
//从一组数中建小顶堆 public static void build_heap2(int[] tree,int n){ = lastnode. 1-n-int; int = PAR (-lastnode. 1) / 2; // start from the last adjustment of non-leaf nodes for (int I = PAR; I> = 0; I -) { heapify2 (Tree, n- , I); } } public static void heap_sort_small (int [] Tree, n-int) { build_heap2 (Tree, n-); // exchange top of the stack and the last element of the current stack, to ensure that the last element in the minimum position, Since the result is not exchanged after the top of the small pile, so to be heapify // second pass, the top element in the stack and exchange the last element of the current, heapify for (int. 1-n-= I; I> = 0; I- -) { the swap (Tree, 0, I); heapify2 (Tree, I, 0); } }
// stack top small insertion public static void heapInsert (int [] Tree, n-int, int NUM) { int I, J; I = n-; J = (n--. 1) / 2; the while (J> = 0 && I = 0!) { IF (Tree [J] <= NUM) BREAK; Tree [I] = Tree [J] ; J = I; int [] = {array2 10, 30, 20 is, 100,40 ,50,14}; J = (. 1-I) / 2; } Tree [I] = NUM; }
// test public static void main (String [] args) { int [] Array 10,4,5,3,1,2 = {}; int = n-be array.length; build_heap2 ( Array, n-); // heapify2 (Array, n-, 0); System.out.println ( "stack building process:"); // stack is built: 2. 3. 1. 5 10. 4 for (int I = 0; I <n-; I ++) { of System.out.print (Array [I] + ""); } System.out.println (); heap_sort_small (Array, n-); // heapify2 (Array, n-, 0) ; System.out.println ( "heap sort result is"); // stack is built: 2. 3. 1. 5 10. 4 for (int I = 0; I <n-; I ++) { of System.out.print ( Array [I] + ""); } System.out.println (); heapInsert (array2,6,14); System.out.println ( "top of the stack is inserted a small number"); // stack is built: 2. 3. 1. 5 10. 4 for (int I = 0; I <array2.length; I ++) { the System.out .print (array2 [I] + ""); } } }

7. Shell sort

thought:

   1. Shell sort is to improve the insertion sort.

   2. The large data set into a plurality of first group (Packet logic), then each team for each insertion sort.

achieve:

{class ShellSort public 
	public static void main (String [] args) { 
		int [] = {3,5,2,7,4,8,3,9} ARR; 
		shellSort (ARR); 
		for (int I = 0; I <arr.length; I ++) { 
			of System.out.print (ARR [I] + ""); 
		} 
	} 
	public static void shellSort (int [] ARR) { 
		int = n-arr.length; 
		// packet, at the beginning of increments of half the length of the array 
		for (int n-GAP = / 2; GAP> 0; GAP / = 2) { 
			// the packet is inserted 
			for (int I = GAP; I <n-; I ++) { 
				// the arr [i] into the correct position 
				iNSERT (ARR, GAP, I); 
			} 
		} 
	} 
	
	Private static void iNSERT (int [] ARR, int GAP, int I) { 
		int value = ARR [i]; 
		int J;  
		for ( j = i-gap; j> = 0 && value <arr [j]; j- = gap) {
			ARR [ GAP + J] = ARR [J]; 
		} 
		ARR [J GAP +] = value;
	}
}

 

 

8. radix sort



9. bucket sort

Guess you like

Origin www.cnblogs.com/yaogungeduo/p/11244277.html