Detailed explanation of JS bubble sort and quick sort
This article mainly shares with you the detailed explanation of Js bubble sort and quick sort. I hope it can help you.
var array = [7, 8, 6, 12, 87, 35, 1, 48, 56, 12, 48, 69, 12, 12, 12, 103, 15, 6, 88, 24, 26, 25, 9, 6];
//冒泡排序 function bubbleSort(arr){ var len = arr.length; for(var i=0; i<len; i++){ var len_j = len - i - 1; for(var j=0; j<len_j; j++){ if(arr[j] > arr[j+1]){ arr[j] = arr[j] ^ arr[j+1]; arr[j+1] = arr[j] ^ arr[j+1]; arr[j] = arr[j] ^ arr[j+1]; } } } } bubbleSort(array); console.log(array); //[1, 6, 6, 6, 7, 8, 9, 12, 12, 12, 12, 12, 15, 24, 25, 26, 35, 48, 48, 56, 69, 87, 88, 103]
//快速排序 function quickSort(arr, low, high){ var staticHigh = high, //获取最初始高位指针 val = arr[low], //把低位当做关键字 index = low; //关键字下标 if(low >= high){ return; } while(low < high){ //如果与关键字相同的,按比关键字大来排序 while(val <= arr[high]){ if(index != high){ //为避免匹配到本身时,错误的把高位下标减1,跳过循环 high--; }else{ break; } } //关键字与高位换位置 arr[index] = arr[high]; arr[high] = val; index = high; while(arr[low] < val){ low++; } //关键字与低位换位置 arr[index] = arr[low]; arr[low] = val; index = low; } quickSort(arr, 0, index-1); //递归前半段 quickSort(arr, index+1, staticHigh); //递归后半段 } quickSort(array, 0, array.length-1); console.log(array); //[1, 6, 6, 6, 7, 8, 9, 12, 12, 12, 12, 12, 15, 24, 25, 26, 35, 48, 48, 56, 69, 87, 88, 103]
Related recommendations:
A simple js bubble sort example
JS bubble sort selection sort and insertion sort example analysis
Detailed explanation of php bubble, selection, insertion and quick sort algorithm
The above is the detailed content of Detailed explanation of JS bubble sort and quick sort. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

What is the difference in the "My Computer" path in Win11? Quick way to find it! As the Windows system is constantly updated, the latest Windows 11 system also brings some new changes and functions. One of the common problems is that users cannot find the path to "My Computer" in Win11 system. This was usually a simple operation in previous Windows systems. This article will introduce how the paths of "My Computer" are different in Win11 system, and how to quickly find them. In Windows1

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

WordPress Website Building Guide: Quickly Build a Personal Website With the advent of the digital age, having a personal website has become fashionable and necessary. As the most popular website building tool, WordPress makes it easier and more convenient to build a personal website. This article will provide you with a guide to quickly build a personal website, including specific code examples. I hope it can help friends who want to have their own website. Step 1: Purchase a domain name and hosting. Before starting to build a personal website, you must first purchase your own

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a
