Five ways to sort arrays in js

The following mainly introduces the five ways of array sorting - sort() method, selection sort, bubble sort, insertion sort and quick sort ,

Friends who are just interested can look down.

1. sort() method in js

Basic idea: Sort the array elements according to the provided sorting rules.
                  To use numerical sorting, it must be called with a function as an argument.

var arr = [123,203,23,13,34,65,65,45,89,13,1];
function func(a,b){
	return a-b;
}
console.log(arr.sort(func)); //(11) [1, 13, 13, 23, 34, 45, 65, 65, 89, 123, 203]

2. Selection sort

Basic idea: First find the smallest (largest) element in the unsorted array and store it at the beginning of the array.
                  Then continue to find the smallest (largest) element from the remaining array elements, return to the end of the sorted array
                  and repeat the second step until all elements are sorted
. Animation demonstration:

var arr = [123,203,23,13,34,65,65,45,89,13,1];
for(var i=0;i<arr.length;i++){
	for(var j=i+1;j<arr.length;j++){
		//如果第一个比第二个大,就交换他们两个位置
		if(arr[i]>arr[j]){
			var temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
	}
}
console.log(arr); //(11) [1, 13, 13, 23, 34, 45, 65, 65, 89, 123, 203]

3. Bubble sort

Basic idea: compare two adjacent numbers at a time. If the positions are not exchanged according to the rules, one comparison can put the largest or smallest value in the last bit of the array and continue to repeat for all
                  elements except the [last bit].
Animation demonstration of the above process :

var arr = [123,203,23,13,34,65,65,45,89,13,1];
for(var i=0; i<arr.length-1; i++){
	//每一轮比较要比多少次
	for(var j=0; j<arr.length-1-i; j++){
	    //如果第一个比第二个大,就交换他们两个位置
	    if(arr[j]>arr[j+1]){
	        var temp = arr[j];
	        arr[j] = arr[j+1];
	        arr[j+1] = temp;
	    }
	}    
}
console.log(arr); //(11) [1, 13, 13, 23, 34, 45, 65, 65, 89, 123, 203]

4. Insertion sort

Basic idea: regard the first element of the array as an ordered sequence, and regard the second element to the last element as an unsorted sequence.
                  Scans the unsorted sequence sequentially from beginning to end, and inserts each scanned element into its proper position in the sorted sequence.
                  If the element to be inserted is equal to an element in the ordered sequence, the element to be inserted is inserted after the equal element.
Animation demo:

var arr = [123,203,23,13,34,65,65,45,89,13,1];
var preIndex, current;
for(var i=1;i<arr.length;i++){
	preIndex = i-1;
	current = arr[i];
	while(preIndex>=0 && arr[preIndex]>current) {
	    arr[preIndex+1] = arr[preIndex];
	    preIndex--;
	}
	arr[preIndex+1] = current;
}
console.log(arr); //(11) [1, 13, 13, 23, 34, 45, 65, 65, 89, 123, 203]

5. Quick sort (relying on recursive function)

Basic idea: Randomly go to a pivot in the known data set,
                  center the rest of the data on the pivot, put the larger ones on the right, and put the smaller ones on the left.
                  Repeat the above two steps for the left and right subsets
. Animation demonstration:

var arr = [123,203,23,13,34,65,65,45,89,13,1];	
//创建快速排序函数
function quickSort(tempArr){
	//递归终止条件
	if(tempArr.length<=1){
		return tempArr;
	};
	//取基准
	var pivotIndex = Math.floor(tempArr.length/2);
	var pivot = tempArr.splice(pivotIndex,1);
	//分左右
	var leftArr = [];
	var rightArr = [];
	for(var i=0;i<tempArr.length;i++){
		if(tempArr[i]>pivot){
			rightArr.push(tempArr[i]);
		}else{
			leftArr.push(tempArr[i]);
	    };
	};
	return quickSort(leftArr).concat(pivot,quickSort(rightArr));
};
console.log(quickSort(arr)); //(11) [1, 13, 13, 23, 34, 45, 65, 65, 89, 123, 203]

 

Guess you like

Origin blog.csdn.net/weixin_55992854/article/details/116849789