C language to write quick sort code

1. Quick Sort

Quicksort is a comparison-based sorting algorithm that can run in O(n log n) time on average, and is one of the most commonly used fast and general-purpose sorting algorithms. The basic idea of ​​this algorithm is to choose an element as the "pivot" (usually the first or last element in the array), then compare all other elements in the array with the pivot, and put the elements less than or equal to the pivot Put into a subarray, put the elements larger than the pivot into another subarray, apply this process recursively to each subarray until the subarray has only zero or one element, and finally concatenate the subarrays in order The sorted array can be obtained.

Specific steps are as follows:

  • Select the pivot element, usually the first or last element.
  • Divide the array into two sub-arrays, place elements less than or equal to the pivot element in the left sub-array, and place elements greater than the pivot element in the right sub-array according to the pivot element.
  • Recursively performs a quicksort operation on the left and right subarrays.
  • Finally, connect the left, pivot, and right parts to get the sorting result.
    The performance of this algorithm depends on the selected pivot element. Ideally, the element in the middle of the array can be selected as the pivot every time. At this time, the time complexity of the quick sort algorithm is o(n log n). However, if the selected pivot is far away from both ends, the efficiency of the algorithm may be reduced, and the time complexity reaches o(n^2). Therefore, in practical applications, some optimization strategies are usually adopted to improve the performance of the algorithm

The following is the quick sort algorithm implemented in C language:

2. Code implementation

#include <stdio.h>  
  
// 交换两个元素的值  
void swap(int *a, int *b) {
    
      
    int temp = *a;  
    *a = *b;  
    *b = temp;  
}  
  
// 分割函数,将数组分成左右两部分,并返回左右指针  
int partition(int arr[], int low, int high) {
    
      
    int pivot = arr[high]; // 选取最后一个元素作为基准值  
    int i = (low - 1); // i指向左边界  
  
    for (int j = low; j < high; j++) {
    
      
        if (arr[j] <= pivot) {
    
      
            i++;  
            int temp = arr[i];  
            arr[i] = arr[j];  
            arr[j] = temp;  
        }  
    }  
  
    int temp = arr[i + 1];  
    arr[i + 1] = arr[high];  
    arr[high] = temp;  
  
    return (i + 1); // 返回基准值的位置  
}  
  
// 快速排序函数  
void quickSort(int arr[], int low, int high) {
    
      
    if (low < high) {
    
      
        int pivot = partition(arr, low, high); // 选取基准值  
        quickSort(arr, low, pivot - 1); // 对左边部分递归排序  
        quickSort(arr, pivot + 1, high); // 对右边部分递归排序  
    }  
}  
  
int main() {
    
      
    int arr[] = {
    
    5, 2, 8, 3, 9, 1, 6};  
    int n = sizeof(arr) / sizeof(arr[0]);  
  
    quickSort(arr, 0, n - 1);  
  
    printf("排序后的数组:\n");  
    for (int i = 0; i < n; i++) {
    
      
        printf("%d ", arr[i]);  
    }  
    printf("\n");  
  
    return 0;  
}

In the above code, the swap function is used to exchange the values ​​of two elements, the partition function is used to divide the array into left and right parts, and returns the left and right pointers, and the quickSort function is the main function of quick sort, which is used to recursively sort the array . In the main function, we define an integer array and call the quickSort function to sort it. Finally, we output the sorted array.

Method Two:

#include <stdio.h>

void quicksort(int *arr, int left, int right) {
    
    
  
  if (left < right) {
    
    

    int pivot = arr[right];
    int i = left - 1;
    int temp;

    for (int j = left; j < right; j++) {
    
    
      if (arr[j] <= pivot) {
    
    
        i++;
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }
    
    i++;
    temp = arr[i];
    arr[i] = pivot;
    arr[right] = temp;

    quicksort(arr, left, i-1);
    quicksort(arr, i+1, right);

  }
}

int main() {
    
    
  
  int arr[] = {
    
    4, 2, 1, 6, 3, 5};
  int n = sizeof(arr)/sizeof(int);

  quicksort(arr, 0, n-1);

  printf("Sorted array: ");
  for (int i = 0; i < n; i++) {
    
    
    printf("%d ", arr[i]);
  }

  return 0;
}

The main body of the quick sort is the quicksort function, which accepts three parameters: an integer array arr, the left boundary left of the array, and the right boundary right of the array. Then recursion is used inside the function for splitting and sorting.

In the quicksort function, first judge whether the left and right pointers are equal, and if not, enter the sorting process.

The element selected as the pivot is arr[right]. During the movement of the i and j pointers, the smaller elements are placed in the interval [left, i-1], and i points to the next position to be exchanged. Finally put the pivot element in place.

Finally, sort the left and right intervals recursively until the task is processed.

When the quicksort function executes, it prints the sorted

Guess you like

Origin blog.csdn.net/fumeidonga/article/details/130332067