GO language commonly used sorting

1. Sort bubble ( the basic idea of bubble sort): The comparison of the key values of adjacent two elements, if in reverse order, the exchange

the BubbleSort FUNC (ARR [] int) { 
	In Flag: = to false 
	// outer control line 
	for I: = 0; I <len (ARR) -1; I ++ { 
		// inner control bar 
		for j: = 0; j < len (ARR) -1-I; J ++ { 
			// compare two adjacent elements 
			IF ARR [J]> ARR [J +. 1] { 
				// exchange data 
				arr [j], arr [j + 1] = arr [ . 1 + J], ARR [J] 
				In Flag to true = 
			} 
		} 
		// determines whether the data is ordered 
		iF! In Flag { 
			return 
		} the else { 
			In Flag to false = 
		} 
	} 
}

 2. Quick Sort

Quick sort (quick sort) is a partition exchange sorting algorithm.

The basic idea: to select a value in the data sequence as a comparison reference value, beginning from alternate ends of each trip data sequence, will be less than the reference value switching element to the front end of the sequence, the reference value is greater than the switching element into the sequence rear end, both the position between the final position becomes the reference value.

the QuickSort FUNC (ARR [] int, int left, int right) { 
	// set reference value 
	TEMP: = ARR [left] 
	index: left = 

	I: left = 
	J: = right 

	for I <= J { 
		// find the right smaller than the reference value data 
		for J> = index && ARR [J]> TEMP = { 
			J, 
		} 
		// Get the value of the reference index suitable 
		IF J> index { 
			ARR [index] = ARR [J] 
			index = J 
		} 
		// Get the value from the left larger than the reference data 
		for I <= index && ARR [I] <TEMP = { 
			I ++ 
		} 
		// Get the value of the reference index suitable 
		IF I <= index { 
			ARR [index] = ARR [I] 
			I = index 
		} 
	} 
	// the reference value in the right position 
	ARR [index] = TEMP 
 
	// recursive call data processing step
	IF-left index> {. 1
		QuickSort(arr, left, index-1)
	}
	if right-index > 1 {
		QuickSort(arr, index+1, right)
	}

}

3. Direct Selection Sort

The basic idea of ​​direct selection sorting (straight select sort): the first pass to select the minimum (or maximum) from the key elements of the data sequence of n elements and into the far first (or last) position, and from the next train n -1 element selected minimum (large) front views of the element and into the (rear) position, and so, after n-1 times to complete the order.

SelectSort FUNC (ARR [] int) { 

	// outer control line 
	for I: = 0; I <len (ARR); I ++ { 
		// record the maximum value of the subscript 
		index: = 0 
		// inner control bar 
		// iterate Find the maximum value of the data 
		for J: =. 1; J <len (ARR) -i; J ++ { 
			IF ARR [J]> arr [index] { 
				// record index 
				index = J 
			} 
		} 

		// exchange data 
		arr [index] , ARR [len (ARR) -1-I] = ARR [len (ARR) -1-I], ARR [index] 
	} 
}

4. heapsort

Heap sort (heap sort) the application is complete binary tree, and its basic ideas: the data sequence "stack" a tree, each trip path traverses only one tree.

// initialize stack 
FUNC HeapInit (ARR [] int) { 

	// Sections were transformed into binary tree root model to achieve a large pile 
	length: = len (ARR) 
	for I: = length / 2 -. 1; I> = 0; i-- { 
		HeapSort (ARR, I,. 1-length) 
	} 

	// root maximum storage 
	for I: = length -. 1; I> 0; i-- { 
		// only if the left child node and the root node with the node under 
		ARR. 1 && I == IF [0] <= ARR [I] { 
			BREAK 
		} 
		// root node and leaf nodes exchange data 
		ARR [0], ARR [I] = ARR [I], ARR [0] 
		HeapSort ( ARR, 0,. 1-I) 
	} 

} 

// Get the maximum in the heap root 
FUNC HeapSort (ARR [] int, int the startNode, maxNode int) { 

	// maximum on root 
	var max int 
	// define do the left child node and the right child node 
	lChild: the startNode * = 2 +. 1 
	rChild: + = lChild. 1 
	// child node exceeds the comparison range out recursively
	lChild IF> = maxNode { 
		return 
	} 
	// find the maximum value of the left and right comparator 
	IF rChild <= maxNode && ARR [rChild]> ARR [lChild] { 
		max = rChild 
	} the else { 
		max = lChild 
	} 

	// node and comparison with 
	if arr [ max] <= ARR [the startNode] { 
		return 
	} 

	// exchange data 
	ARR [the startNode], ARR [max] = ARR [max], ARR [the startNode] 
	// recursive next comparison 
	HeapSort (ARR, max, maxNode) 
}

The insertion sort

InsertSort FUNC (ARR [] int) { 
	for I: =. 1; I <len (ARR); I ++ { 
		// if the current data is less than ordered data 
		IF ARR [I] <ARR [-I. 1] { 
			J: I = --1 
			// obtain valid data 
			TEMP: = ARR [I] 
			@ a more orderly data 
			for J> = 0 && ARR [J]> TEMP { 
				ARR [J +. 1] = ARR [J] 
				J, 
			} 
			ARR [+ J. 1] = TEMP 
		} 
	} 
}

 6. Shell sort

Hill sorting (shell sort), also known as narrow increment sort, it's the basic idea: a packet direct insertion sort.

ShellSort FUNC (ARR [] int) { 
	// The delta (len (arr) / 2) each time into the 1/2 
	for inc is: = len (ARR) / 2; inc is> 0; inc is / = {2 

		for I: = inc is; I <len (ARR); I ++ { 
			TEMP: = ARR [I] 

			// 0 compares the data according to the increment 
			for j: = i - inc; j> = 0; j - inc is {= 
				// the condition data exchange 
				IF TEMP <ARR [J] { 
					ARR [J], ARR [J + inc is] = ARR [J + inc is], ARR [J] 
				} the else { 
					BREAK 
				} 
			} 
		} 
	} 
}

 7. The binary search the BinarySearch (data elements) returns a value of the subscript

main Package 

Import "FMT" 

FUNC the BinarySearch (ARR [] int, int NUM) {int 
	// define the starting index 
	start: = 0 
	superscript end // define 
	End: = len (ARR) -. 1 
	// intermediate reference value 
	MID: = (Start + End) / 2 

	for I: = 0; I <len (ARR); I ++ { 
		// find the value of the reference value 
		IF NUM == ARR [MID] { 
			return MID 
		} the else IF NUM> ARR [ mID] { 
			// find the right value larger than the reference 
			Start = mID. 1 + 
		} the else { 
			// smaller than the reference value to find the left 
			End mID = -. 1 
		} 
		// set the reference value intermediate position again 
		mid = (start + end) / 2 
	} 
	return -1 
} 
FUNC main () { 
	// the premise must be "ordered data" 
	ARR: = [] {int. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10}
	num := 666

	index := BinarySearch(arr, num)
	fmt.Println(index)
}

 8. Sorting disguised

Disguised ranking is based on a large number of repeats in the range of a

main02 FUNC () { 
	// random number seed 
	rand.Seed (Time.now () UnixNano ().) 
	S: = the make ([] int, 0) 

	for I: = 0; I <10000; I ++ { 
		S = the append (S, rand.Intn (1000)) // 0-999 
	} 
	fmt.Println (S) 

	the number of data set statistics appear @ 
	m: = the make (Map [int] int) 
	for I: = 0; I <len (S); I ++ { 
		m [S [I]] ++ 
	} 
	//fmt.Println(m) 

	// Sort 
	for I: = 0; I <1000; I ++ { 
		for J: = 0; J <m [I]; J ++ { 
			fmt.Print (I, "") 
		} 
	} 

}

 

Guess you like

Origin www.cnblogs.com/lurenq/p/12089175.html