How to sort using quick sort in C language
Arrangement method of quick sorting method: First, set a reference point every time you sort, put all the numbers less than or equal to the reference point to the left of the reference point; then put all the numbers greater than or equal to the reference point To the right of the reference point; finally, every exchange will not be like bubble sorting where only adjacent numbers can be exchanged each time, and the exchange distance will be much larger.
Quick sorting method:
Algorithm idea:
(1) We select a record (usually the first one) from the record sequence to be sorted as the reference element (called key) key=arr[left], and then set two variables, left points to the leftmost part of the sequence , right points to the rightmost part of the data.
(2) key is first compared with arr[right]. If arr[right] (3) If there is arr[right] (4) Then move right and repeat the above steps (5) Finally get {23 58 13 10 57 62} 65 {106 78 95 85}, and then perform the same operation on the left subarray and right subarray. Finally, an ordered sequence is obtained. Algorithm implementation: Related learning recommendations: C video tutorialpublic class QuickSort {
public static void quickSort(int [] arr,int left,int right) {
int pivot=0;
if(left<right) {
pivot=partition(arr,left,right);
quickSort(arr,left,pivot-1);
quickSort(arr,pivot+1,right);
}
}
private static int partition(int[] arr,int left,int right) {
int key=arr[left];
while(left<right) {
while(left<right && arr[right]>=key) {
right--;
}
arr[left]=arr[right];
while(left<right && arr[left]<=key) {
left++;
}
arr[right]=arr[left];
}
arr[left]=key;
return left;
}
public static void main(String[] args) {
int arr[]= {65,58,95,10,57,62,13,106,78,23,85};
System.out.println("排序前:"+Arrays.toString(arr));
quickSort(arr,0,arr.length-1);
System.out.println("排序后:"+Arrays.toString(arr));
}
}
排序前:[65, 58, 95, 10, 57, 62, 13, 106, 78, 23, 85]
排序后:[10, 13, 23, 57, 58, 62, 65, 78, 85, 95, 106]
The above is the detailed content of How to sort using quick sort in C language. 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



Master the key skills and precautions of Java quick sort. Quick sort (QuickSort) is a commonly used sorting algorithm. Its core idea is to divide the sequence to be sorted into two independent parts by selecting a benchmark element, and all the elements in one part are equal. is less than the base element, and all elements of the other part are greater than the base element, then the two parts are recursively sorted, and finally an ordered sequence is obtained. Although quick sort has a time complexity of O(nlogn) in the average case, it degenerates to O(nlogn) in the worst case

How to implement quick sorting in Python: 1. Define a function called quick_sort and use the recursive method to implement quick sorting; 2. Check the length of the array. If the length is less than or equal to 1, return the array directly. Otherwise, select the array. The first element is used as the pivot element (pivot), and then the array is divided into two sub-arrays smaller than the pivot element and larger than the pivot element; 3. Connect the two sub-arrays and the pivot element to form a sorted array. .

Java implementation of quick sort and its performance analysis Quick sort (QuickSort) is a very commonly used and efficient sorting algorithm. It is a divide and conquer (Divide and Conquer) idea. This algorithm divides an array into two sub-arrays, then sorts the two sub-arrays respectively, and finally turns the entire array into an ordered sequence. Quick sort shows excellent performance when processing large-scale data. Quick sort is implemented recursively. The basic idea is as follows: Choose a base

Quick sort method: 1. Create a Java sample file; 2. Implement the quick sort algorithm through the quickSort method; 3. Select an element in the array as the pivot (pivot), and divide the array into two sub-arrays, one containing the pivot The element with the smaller element is the smaller element, and the other contains the element that is larger than the pivot element, and then the quick sort algorithm is recursively applied to the two sub-arrays; 4. Sort the array in the main method and output the result.

Quicksort is a frequently used sorting algorithm due to its popularity and popularity relative to other sorting algorithms.

PHP is a very popular programming language and it is widely used for web development. In PHP, array is a very common data type and a very powerful data structure. Because of this, PHP provides many array functions to help developers handle and manipulate arrays. This includes the quick sort function, which helps us sort arrays quickly. Quick sort is a common sorting algorithm. Its basic idea is to divide an array into two sub-arrays, one smaller than the other, through comparison and exchange, and then recursively

How to implement the quick sort algorithm in Java Quick sort (QuickSort) is a commonly used and efficient sorting algorithm. Its basic idea is to use the divide and conquer strategy (Divide and Conquer). By selecting one element at a time as the benchmark value, the array to be sorted is divided into two parts, one part is smaller than the benchmark value, and the other part is larger than the benchmark value, and then the two parts are processed separately. Recursive sorting, and finally achieve sorting of the entire array. Below we will introduce in detail how to use Java language to achieve rapid sorting

The implementation principle and optimization of Java quick sort function. Quick sort is an efficient sorting algorithm. Its implementation idea is to divide a large problem into multiple small problems through the divide and conquer method, and solve the sub-problems recursively to finally obtain the overall solution. . In quick sort, we need to select a benchmark element and divide the array into two parts, one part is smaller than the benchmark element, and the other part is larger than the benchmark element. The two parts are then quickly sorted again until there is only one element per subproblem. Finally, the solutions to all subproblems are combined to obtain the array's
