Sorting method enumeration

Posted by reli4nt on Fri, 29 Oct 2021 12:01:21 +0200

Sorting method

Stable: after sorting, the positions of numbers of the same size will not be changed

methodthinkingTime complexityIs it stable
Bubble sortingStart the comparison from the start or end position. If the value is larger or smaller than him, switch and determine a position at a timeO(n2)yes
Quick sortAt the beginning, take a number, put the smaller one on the left and the larger one on the right, and then distinguish the left until the end of the row, then the right until the endO(nlogn)no
Select sortLoop through to find the largest or smallest one and put it in the first positionO(n2)no
Insert sortJudge the data in the unordered array and insert it into the sorted array one by oneO(n2)yes
Shell sortFirst, the whole sequence to be sorted is divided into several subsequences for direct insertion sorting. When the records in the whole sequence are "basically orderly", all records are directly inserted and sorted in turnO(n3/2)No (the insertion sequence of each paragraph is stable, and the whole is not necessarily)
Merge sortThe principle of merging algorithm is to divide all elements into adjacent pairs, then sort them in pairs, and then merge and sort the adjacent pairs of elements. In this cycle, only two groups of the largest ordered arrays are left to mergeO(nlogn)yes
  1. Bubble sorting
    Start the comparison from the start or end position. If the value is larger or smaller than him, switch and determine a position at a time
let arr = [1,2,3,6,7,9,0,9,8,7,8,9,6,5,4,3,2,6,7,5,3,3,5];
let maopao = function(arr = []){
	// Avoid affecting the original array
	arr = JSON.parse(JSON.stringify(arr));
	let length = arr.length;
	for(let i = 0; i < length; i++){
		for(let j = i+1; j < length; j++){
			console.log(arr[i],arr[j])
			if(arr[i] > arr[j]){
				let str = arr[i];
				arr[i] = arr[j];
				arr[j] = str;
			}
		}
	}
	return arr;
}
maopao(arr);
  1. Quick sort
    At the beginning, take a number, put the smaller one on the left and the larger one on the right, and then distinguish the left until the end of the row, then the right until the end
let arr = [1,2,3,6,7,9,0,9,8,7,8,9,6,5,4,3,2,6,7,5,3,3,5];
let quickSort = function(arr){
	arr = JSON.parse(JSON.stringify(arr));
	let length = arr .length;
	if(length == 0) return arr ;
	let nowMiddle = arr .splice(Math.floor(length/2),1)[0];
	let left = [];
	let right = [];
	for(let i=0; i<length-1;i++){
		if(arr [i]<nowMiddle ){
			left.push(arr [i]);
		} else {
			right.push(arr [i]);
		}
	}
	return quickSort(left).concat([nowMiddle],quickSort(right));
}
quickSort(arr);
  1. Select sort
    Loop through to find the largest or smallest one and put it in the first position
let arr = [1,2,3,6,7,9,0,9,8,7,8,9,6,5,4,3,2,6,7,5,3,3,5];
let chooseSort = function(arr = []){
	arr = JSON.parse(JSON.stringify(arr));
	let length = arr.length;
	if(length == 0) return arr;
	// The last one doesn't need comparison
	for(let i = 0; i<length-1; i++){
		let nowIndex = i;
		// It is better to record subscripts than to exchange values directly
		for(let j = i+1; j<length; j++){
			if(arr[nowIndex] > arr[j]){
				nowIndex = j;
			}
		}
		if(nowIndex !=i){
			let nowSmall = arr[nowIndex];
			arr[nowIndex] = arr[i];
			arr[i] = nowSmall;
		}
	}
	return arr;
}
chooseSort(arr);
  1. Insert sort
    Judge the data in the unordered array and insert it into the sorted array one by one
// Find where it should be inserted
let searchInsertIndex = function(arr,item){
	let left = 0;
	let right = arr.length -1;
	while(left <= right){
		let middle = Math.floor((left + right)/2);
		if(arr[middle] == item){
			// At this time, the same data has been found. It can be determined that the position is in the middle, but it cannot be inserted behind the middle, because it is unknown whether there is no duplicate data in the current array. Now it is necessary to solve the duplicate data
			if(arr[middle+1] > item){
				return middle; // If you don't need stability, just this line
			} else {
				left = left + 2;
			}
		} else if(arr[middle] < item){
			if(arr[middle+1] > item){
				return middle;
			} else if(arr[right] <= item){
				return right;
			}
			left = middle+1; // If you don't need stability, just this line
		} else if(arr[middle] > item){
			if(arr[middle-1] < item){
				return middle - 1;
			} else if(arr[left] > item){
				return left - 1;
			}
			right = middle-1; // If you don't need stability, just this line
		}
	}
}
let arr = [1,2,3,6,7,9,0,9,8,7,8,9,6,5,4,3,2,6,7,5,3,3,5];
// Additional space required
let insertSort = function(){
	let extraArr = [];
	let length = arr.length;
	for(let i=0; i<length; i++){
		let item = arr[i];
		let extraLength = extraArr.length;
		// When the data length is greater than 4, the dichotomy query insertion is used
		// When inserting, you need to determine which position to insert
		// Find the specific insertion position according to the dichotomy
		let index = searchInsertIndex(extraArr,item);
		extraArr.splice(index+1,0,item);
	}
	return extraArr;
}
insertSort(arr);

... to be continued

Topics: Javascript Algorithm