Quick Sort Algorithm

快速排序算法实现代码:

//============================================================================
// Name        : QuickSort.cpp
// Author      : Danny
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

void swap(int a[], int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

int position(int a[], int left, int right) {
    int pivot = left;
    int i = left;
    int j = right;
    while (i < j) {

        while (i < j && a[j] >= a[pivot]) {
            j--;
        }

        if (i < j && a[j] < a[pivot]) {
            swap(a, pivot, j);
            pivot = j;
        }

        while (i < j && a[i] <= a[pivot]) {
            i++;
        }
        if (i < j && a[i] > a[pivot]) {
            swap(a, pivot, i);
            pivot = i;
        }

    }
    return pivot;
}

void quickSort(int a[], int left, int right) {
    if (left >= right)
        return;
    int pos = position(a, left, right);
    quickSort(a, left, pos - 1);
    quickSort(a, pos + 1, right);

}

int main() {
    int a[8] = { 1,2,4,3,3,5,9,0};
    quickSort(a, 0, 7);
    for (int i = 0; i < 8; i++) {
        cout << a[i] << endl;
    }
    return 0;
}

Java实现代码:

排序类:

package algorithm;

public class SortAlgorithm {
    void quickSort(int a[], int left, int right) {
        if (left >= right)
            return;
        int pos = position(a, left, right);
        quickSort(a, left, pos - 1);
        quickSort(a, pos + 1, right);

    }

    void swap(int a[], int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    int position(int a[], int left, int right) {
        int pivot = left;
        int i = left;
        int j = right;
        while (i < j) {

            while (i < j && a[j] >= a[pivot]) {
                j--;
            }

            if (i < j && a[j] < a[pivot]) {
                swap(a, pivot, j);
                pivot = j;
            }

            while (i < j && a[i] <= a[pivot]) {
                i++;
            }
            if (i < j && a[i] > a[pivot]) {
                swap(a, pivot, i);
                pivot = i;
            }

        }
        return pivot;
    }

}

主方法类:

package algorithm;

public class QuickSort {

    public static SortAlgorithm sa;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        sa=new SortAlgorithm();
        int[] a={4,2,1,6,3};
        sa.quickSort(a, 0, 4);
        for(int i:a){
            System.out.println(i);
        }
    }
}
时间: 2024-10-10 20:17:19

Quick Sort Algorithm的相关文章

1101. Quick Sort (25)

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. G

pat1101. Quick Sort (25)

1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements

1101. Quick Sort (25)【快排】——PAT (Advanced Level) Practise

题目信息 1101. Quick Sort (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are

PAT 1101 Quick Sort[一般上]

1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the

1101 Quick Sort (25 分)递推

1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than th

1101 Quick Sort (25 分)

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. G

PAT_A1101#Quick Sort

Source: PAT A1101 Quick Sort (25 分) Description: There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its lef

PAT甲级——A1101 Quick Sort

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. G

Collection of algorithm for sorting. 常见排序算法集(三) —— Quick Sort

Quick Sort 快排,一个排序方法能直接享受这样的名称殊荣,呵呵,可见其威力与重要性. 其中最重要的思想就是 "分治"-- divide and conquer ! 这里排序用到的思想极其简单,却是很实用的!小孩子都会的简单想法. 先把所有数据分成三个部分. 在所有数据中选取某一元素X,比X小的放左边,比X大的放右边. 接着把这一思想同样分别施加在X元素的左边和右边部分,同样继续划分,选出一个元素X' 比X'小的放左边比X'大的放右边. 继续施加这种划分策略,直到划分的元素很少(