JavaScript sorting algorithm implementation

Preface

Speaking of sorting, it is definitely a word that must appear in the data structure textbook of every computer-related major. The last question on the final exam paper is also related to this. Remember that I was still a sophomore at the time, and I was still so... There are also many sorts learned in the textbook, we generally have the following

Insert picture description here

Bubble Sort

Bubble sort is a simple sorting algorithm. It has repeatedly visited the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until no more exchanges are needed, which means that the sequence has been sorted. The origin of the name of this algorithm is because the smaller the element will slowly "float" to the top of the sequence through exchange.

Insert picture description here
Code

	const bubbleSort = arr => {
    
    
			///轮数
			for (let i = 0; i < arr.length - 1; i++) {
    
    
				//次数
				for (let j = 0; j < arr.length - 1 - i; j++) {
    
    

					//判断前一个大于后一个数时进行交换
					if (arr[j] > arr[j + 1]) {
    
    
						//借助第三方变量交换两个变量的值
						let temp = arr[j];
						arr[j] = arr[j + 1];
						arr[j + 1] = temp;
					}
				}
			}
			return arr
		}
		const arr = [32, 4, 67, 82, 21, 11];
		console.log(bubbleSort(arr));

Optimizing and updating...

Insertion sort

Although the code implementation of insertion sort is not as simple and crude as bubble sort and selection sort, its principle should be the easiest to understand, because anyone who has played poker should be able to understand it in seconds. It works by constructing an ordered sequence. For unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert it. Insertion sorting is usually implemented by in-place sorting (that is, sorting that only needs O(1) extra space), so in the process of scanning from back to forward, it is necessary to repeatedly shift the sorted elements backwards. , To provide insertion space for the latest elements.

Code

		const insertSort = arr => {
    
    
			for(let i = 0; i < arr.length; i++){
    
    
				let temp = arr[i];
				for(let j = 0; j < i; j++){
    
    
					if(temp < arr[j] && j === 0){
    
    
						arr.splice(i, 1);
						arr.unshift(temp);
						break;
					}else if(temp > arr[j] && temp < arr[j+1] && j < i - 1){
    
    
						arr.splice(i, 1);
						arr.splice(j+1, 0, temp);
						break;
					}
				}
			}
			return arr;
		}

Optimizing and updating...

Select sort

Select sorting, that is, select the smallest each time, and then change the position, that is, first find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, and then continue to find the smallest from the remaining unsorted elements The (large) element is then placed at the end of the sorted sequence. And so on, until all elements are sorted.

	
		let selectionSort = arr => {
    
    
			
			var len = arr.length;
			var minIndex, temp;
			console.time('选择排序耗时');
			for (var i = 0; i < len - 1; i++) {
    
    
					minIndex = i;
					for (var j = i + 1; j < len; j++) {
    
    
							if (arr[j] < arr[minIndex]) {
    
         //寻找最小的数
									minIndex = j;                 //将最小数的索引保存
							}
					}
					temp = arr[i];
					arr[i] = arr[minIndex];
					arr[minIndex] = temp;
			}
			console.timeEnd('选择排序耗时');
			return arr;
		}

Continuously updating...

Guess you like

Origin blog.csdn.net/pz1021/article/details/105016321