图片来源:medium
排序是数据结构和算法中最重要的部分之一。排序算法有很多种,这是最简单的算法之一:冒泡排序。
排序算法是计算机科学的基础,而冒泡排序是最简单、最直观的排序算法之一。这篇文章将探讨冒泡排序的工作原理,分析其时间复杂度,并演练 JavaScript 实现。
在本系列中,我将分享使用 Javascript 的完整排序算法数据结构和算法,并从冒泡排序开始。如果您喜欢并希望我通过示例分享完整的排序算法,请喜欢并关注我。它激励我为你们创建和准备内容。
冒泡排序是一种简单的排序算法,它重复遍历列表,比较相邻元素(下一个元素),如果顺序错误则交换它们。重复此过程直到列表排序完毕。该算法因其较小的元素“冒泡”到列表顶部而得名。
让我们深入代码看看冒泡排序是如何在 JavaScript 中实现的:
// By default ascending order function bubble_sort(array) { const len = array.length; // get the length of an array //The outer loop controls the inner loop, which means the outer loop will decide how many times the inner loop will be run. //If the length is n then the outer loop runs n-1 times. for (let i = 0; i < len - 1; i++) { // Inner loop will run based on the outer loop and compare the value, //If the first value is higher than the next value then swap it, loop must go on for each lowest value for (let j = 0; j > len - i -1; j++) { // checking if the first element greater than to the next element if (array[j] > array[j + 1]) { // then, swap the value array[j] to array[j+1] let temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } return array; // return the sorted array; } const array = [7, 12, 9, 11, 3]; // input data console.log(bubble_sort(array)); // output data after sorted! // [3, 7, 9, 11, 12];
// Descending order function bubble_sort_descending_order(array) { const len = array.length; for (let i = 0; i < len - 1; i++) { for (let j = 0; j < len - i -1; j++) { // checking if first element greter than next element, if (array[j] < array[j + 1]) { // then, swap the value array[j] to array[j+1] let temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } return array; } const array = [7, 12, 9, 11, 3]; // input data console.log(bubble_sort_descending_order(array)); // output data after sorted! // [ 12, 11, 9, 7, 3 ]
已经添加了注释并解释了上面的每一行代码。但我也会详细解释,以帮助您理解完整的流程和代码。
// By default ascending order function bubble_sort(array) { const len = array.length; // get the length of an array //The outer loop controls the inner loop, which means the outer loop will decide how many times the inner loop will be run. //If the length is n then the outer loop runs n-1 times. for (let i = 0; i < len - 1; i++) { // Inner loop will run based on the outer loop and compare the value, //If the first value is higher than the next value then swap it, loop must go on for each lowest value for (let j = 0; j > len - i -1; j++) { // checking if the first element greater than to the next element if (array[j] > array[j + 1]) { // then, swap the value array[j] to array[j+1] let temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } return array; // return the sorted array; } const array = [7, 12, 9, 11, 3]; // input data console.log(bubble_sort(array)); // output data after sorted! // [3, 7, 9, 11, 12];
// Descending order function bubble_sort_descending_order(array) { const len = array.length; for (let i = 0; i < len - 1; i++) { for (let j = 0; j < len - i -1; j++) { // checking if first element greter than next element, if (array[j] < array[j + 1]) { // then, swap the value array[j] to array[j+1] let temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } return array; } const array = [7, 12, 9, 11, 3]; // input data console.log(bubble_sort_descending_order(array)); // output data after sorted! // [ 12, 11, 9, 7, 3 ]
// optimized version: function bubble_sort(array) { const len = array.length; // get the length of the array //The outer loop controls the inner loop, which means the outer loop will decide how many times the inner loop will be run. //If the length is n then the outer loop run n-1 times. for (let i = 0; i < len - 1; i++) { // Inner loop will run based on the outer loop and compare the value, //If the first value is higher than the next value then swap it, loop must go on for each lowest value let isSwapped = false; for (let j = 0; j < len - i -1; j++) { //check if the first element is greater than the next element if (array[j] > array[j + 1]) { // then, swap the value array[j] to array[j+1] let temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; isSwapped = true; } } //If no element swap by inner loop then break; if (isSwapped === false) { break; } } return array; } const array = [7, 12, 9, 11, 3]; // input data console.log(bubble_sort(array)); // output data after sorted! // [3, 7, 9, 11, 12];
在最坏和平均情况下,冒泡排序的时间复杂度为 (O(n²)),其中 (n) 是数组中元素的数量。这是因为每个元素都会与其他元素进行比较。在最好的情况下,当数组已经排序时,如果添加优化以在不需要交换时停止算法,时间复杂度可以是 (O(n))。
在最好的情况下,当数组已经排序时,由于 isSwapped 优化,算法可以提前终止,导致时间复杂度为 (O(n))。
总体而言,由于其二次时间复杂度,冒泡排序对于大型数据集效率不高,但它对于小型数组很有用,或者作为理解排序算法的教育工具。
冒泡排序由于其简单性而成为一种用于教育目的的优秀算法。然而,由于其二次时间复杂度,它不适合大型数据集。尽管冒泡排序效率低下,但理解冒泡排序为学习更高级的排序算法奠定了基础。
以上是了解冒泡排序算法:分步指南的详细内容。更多信息请关注PHP中文网其他相关文章!