Home Backend Development C#.Net Tutorial How to sort using quick sort in C language

How to sort using quick sort in C language

Aug 08, 2020 am 10:13 AM
Quick sort

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.

How to sort using quick sort in C language

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.

How to sort using quick sort in C language

(2) key is first compared with arr[right]. If arr[right]key, then we only need to compare right--, right--, and then compare arr[right] with key until arr[right]

How to sort using quick sort in C language

(3) If there is arr[right]key, then arr[right]=arr[left]. If arr[left]

How to sort using quick sort in C language

(4) Then move right and repeat the above steps

How to sort using quick sort in C language

(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.

How to sort using quick sort in C language

Algorithm implementation:

public 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));
   }
}
Copy after login
排序前:[65, 58, 95, 10, 57, 62, 13, 106, 78, 23, 85]
排序后:[10, 13, 23, 57, 58, 62, 65, 78, 85, 95, 106]
Copy after login

Related learning recommendations: C video tutorial

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java quick sorting tips and precautions Java quick sorting tips and precautions Feb 25, 2024 pm 10:24 PM

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 sort using Python How to implement quick sort using Python Dec 18, 2023 pm 03:37 PM

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. .

Quick sort algorithm implemented in Java and its efficiency evaluation Quick sort algorithm implemented in Java and its efficiency evaluation Feb 18, 2024 pm 03:38 PM

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

How to quickly sort functions in java How to quickly sort functions in java Jan 18, 2024 pm 05:26 PM

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.

What is quick sort in C language? What is quick sort in C language? Sep 26, 2023 am 11:00 AM

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

Quick sort using array functions in PHP Quick sort using array functions in PHP Jun 16, 2023 am 08:54 AM

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 quick sort algorithm using java How to implement quick sort algorithm using java Sep 19, 2023 am 11:28 AM

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

Optimization and implementation principles: Quick sort in Java Optimization and implementation principles: Quick sort in Java Feb 20, 2024 pm 01:24 PM

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

See all articles