zoukankan      html  css  js  c++  java
  • 快排

     1 #include <string.h>
    2 #include <stdio.h>
    3
    4 void Swap(int * a, int i, int j)
    5 {
    6 int temp = a[i];
    7 a[i] = a[j];
    8 a[j] = temp;
    9 }
    10
    11 int Partition(int * a, int start, int end)
    12 {
    13 int sentinal = a[start];
    14 while (start < end) {
    15 while (a[end] > sentinal && start < end) {
    16 --end;
    17 }
    18 a[start] = a[end];
    19 while (a[start] <= sentinal && start < end) {
    20 ++start;
    21 }
    22 a[end] = a[start];
    23 }
    24 a[start] = sentinal;
    25 return start;
    26 }
    27
    28 void QuickSort(int * a, int start, int end)
    29 {
    30 if (start >= end)
    31 return;
    32 int partition = Partition(a, start, end);
    33 QuickSort(a, start, partition - 1);
    34 QuickSort(a, partition + 1, end);
    35 }
    36 void Print(int * a, int n)
    37 {
    38 int i;
    39 for (i = 0; i < n; ++i) {
    40 printf("%3d", a[i]);
    41 }
    42 printf("\n");
    43 }
    44
    45
    46 int main()
    47 {
    48 int a[10] = {1, 9, 8, 7, 3, 5, 6, 2, 0, 4};
    49 Print(a, 10);
    50 QuickSort(a, 0, 9);
    51 Print(a, 10);
    52
    53 return 0;
    54 }
  • 相关阅读:
    Spinlock
    Leetcode: Summary Ranges
    Leetcode: Search a 2D Matrix
    Leetcode: Rotate Image
    Leetcode: Length of Last Word in python
    BBC-unit7 session1
    BBC-unit6 session5
    BBC-unit6 session4
    BBC英语-unit6 session3
    BBC英语-unit6 session2
  • 原文地址:https://www.cnblogs.com/tzhangofseu/p/2221201.html
Copyright © 2011-2022 走看看