JavaScript 寻找数组中的第N大的元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>寻找数组中第K大的元素</title> </head> <body> <h3>寻找数组中第K大的元素</h3> <script> // 冒泡排序法 function bubblingQueryArrIndex(arr,index) { if (arr.length == 0) { return; } if (index > arr.length - 1 || index - 1 < 0) { return; } //冒泡排序 for (let i = 0; i < arr.length - 1; i ++ ) { var flag = 0 for (let j = 0; j < arr.length - i - 1; j++) { if (arr[j] < arr[j + 1]) { var temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; flag = 1; } } if (flag == 0) { break; } } console.log(arr); return arr[index - 1]; } console.log(bubblingQueryArrIndex([2,6,8,1,9,45,3],1)); //选择排序法 function chooseQueryArrIndex(arr,index) { if (arr.length == 0) { return; } if (index > arr.length - 1 || index - 1 < 0) { return; } for (let i = 0; i < arr.length; i ++) { for (let j = i + 1; j < arr.length; j ++) { if (arr[i] < arr[j]) { var temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } } console.log(arr); return arr[index - 1]; } console.log(chooseQueryArrIndex([1,4,6,8,9,12,34,56,78],3)); //插入排序法 解决 function insertQueryArrIndex(arr,index) { if (arr.length == 0) { return; } if (index > arr.length - 1 || index - 1 < 0) { return; } for (let i = 1; i < arr.length; i ++) { var current = arr[i]; var preIndex = i - 1; while (preIndex >= 0 && arr[preIndex] < current) { arr[preIndex + 1] = arr[preIndex]; preIndex --; } arr[preIndex + 1] = current; } console.log(arr); return arr[index - 1]; } console.log(insertQueryArrIndex([1,4,6,8,9,12,34,56,78],3)); //二分插入排序 function binaryInsertQueryIndex(arr,index) { if (arr.length == 0) { return; } if (index > arr.length - 1 || index - 1 < 0) { return; } for (let i = 1; i < arr.length; i ++) { var current = arr[i]; var left = 0; var right = i - 1; while (left <= right) { var mid = parseInt((left + right) / 2); if (arr[mid] < current) { right = mid - 1; }else { left = mid + 1; } } for (let j = i - 1; j >= left; j--) { arr[j + 1] = arr[j]; } arr[left] = current; } console.log(arr); return arr[index - 1]; } console.log(binaryInsertQueryIndex([1,4,6,8,9,12,34,56,78],3)); //简化排序 只排前Index的数组 function mySort(arr,index) { if (arr.length == 0) { return; } if (index > arr.length - 1 || index - 1 < 0) { return; } for (let i = 0; i < arr.length; i ++) { if (i < index) { //排序 这里采用选择排序法 for (let j = i + 1; j < index; j ++) { if (arr[i] < arr[j]) { var temp = arr[j] arr[j] = arr[i]; arr[i] = temp; } } }else { if (arr[i] > arr[index - 1]) { arr[index - 1] = arr[i]; //再对前面的进行排序 for (let k = 0; k < index; k++) { for (let s = k + 1; s < index; s++) { if (arr[k] < arr[s]) { var temp = arr[s]; arr[s] = arr[k]; arr[k] = temp; } } } }else { continue; } } } console.log(arr); return arr[index - 1]; } console.log(mySort([1,4,6,8,89,9,12,34,56,78],3)); function myInsertSort(arr,index) { if (arr.length == 0) { return; } if (index > arr.length - 1 || index - 1 < 0) { return; } var sortArr = []; for (let i = 0; i < arr.length; i ++) { var temp = arr[i]; if (i == 0) { sortArr[i] = temp; } if (i < index) { sortArr[i] = temp; //排序 var preIndex = i - 1; var current = sortArr[i]; while (preIndex >= 0 && sortArr[preIndex] < current) { sortArr[preIndex + 1] = sortArr[preIndex]; preIndex --; } sortArr[preIndex + 1] = current; }else { if (temp > sortArr[index - 1]) { sortArr[index - 1] = temp; //排序 var preIndex = index - 1 - 1; var current = sortArr[index - 1]; while (preIndex >= 0 && sortArr[preIndex] < current) { sortArr[preIndex + 1] = sortArr[preIndex]; preIndex --; } sortArr[preIndex + 1] = current; }else { continue; } } } console.log(sortArr); return sortArr[index - 1]; } console.log(myInsertSort([1,4,6,8,89,9,12,34,56,78],3)); </script> </body> </html>
后面应该还有更加简洁的算法 以后会补充