对数组进行快速排序

引用:http://www.zhimengzhe.com/bianchengjiaocheng/cbiancheng/316948.html

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void quickSort(vector<int> &a, int, int);
void swap(int &a, int&b);
vector<string> split(string s, string seperator);

int main() {
    string str;
    cout << "please input your array: " << endl;
    getline(cin, str);
    vector<string> strs = split(str, " ");
    cout << "The original array is " << endl;
    for (unsigned int i = 0; i < strs.size(); i++) {
        cout << strs[i] << " ";
    }
    cout << endl;
    vector<int> array(strs.size());
    for (unsigned int i = 0; i < strs.size(); i++) {
        array[i] = atoi(strs[i].c_str());
    }
    int len = array.size();
    cout << "The ordered array is " << endl;
    quickSort(array, 0, len-1);
    for (int i = 0; i < len; i++) {
        cout << array[i] << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}
void quickSort(vector<int> &a, int start, int base) {
    if (start >= base) {
        return;
    }
    int i = start, j = start;
    int temp = a[base];
    for (;j<base;j++) {
        if (a[j]<=temp) {
            swap(a[i], a[j]);
            i++;
        }
    }
    if (a[i] > a[base]) {
        swap(a[i], a[base]);
    }
    quickSort(a, start, i - 1);
    quickSort(a, i + 1, base);
}
void swap(int &a, int&b) {
    if (a == b) {
    }
    else {
        a = a + b;
        b = a - b;
        a = a - b;
    }

}
vector<string> split(string s, const string pattern) {
    string::size_type pos;
    vector<string> result;
    s += pattern;
    unsigned int size = s.size();
    for (unsigned int i = 0; i < size; i++) {
        pos = s.find(pattern, i);
        if (pos < size) {
            string str = s.substr(i, pos - i);
            if (!str.empty()){
                result.push_back(str);
            }
            i = pos + pattern.size() - 1;

        }
    }
    return result;
}
### 回答1: public static void quickSort(int[] arr, int left, int right) { if (arr == null || left >= right) { return; } int pivot = arr[left]; int i = left, j = right; while (i < j) { while (i < j && arr[j] >= pivot) { j--; } arr[i] = arr[j]; while (i < j && arr[i] <= pivot) { i++; } arr[j] = arr[i]; } arr[i] = pivot; quickSort(arr, left, i - 1); quickSort(arr, i + 1, right); } ### 回答2: 以下是一段Java代码示例,用于对数组进行快速排序: ```java public class QuickSort { public static void quickSort(int[] array, int low, int high) { if (low < high) { int pivot = partition(array, low, high); quickSort(array, low, pivot - 1); quickSort(array, pivot + 1, high); } } public static int partition(int[] array, int low, int high) { int pivot = array[high]; int i = low - 1; for (int j = low; j < high; j++) { if (array[j] < pivot) { i++; swap(array, i, j); } } swap(array, i + 1, high); return i + 1; } public static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void main(String[] args) { int[] array = {7, 2, 6, 4, 1, 8, 5, 3}; int n = array.length; System.out.println("原始数组:"); for (int i : array) { System.out.print(i + " "); } quickSort(array, 0, n - 1); System.out.println("\n排序后的数组:"); for (int i : array) { System.out.print(i + " "); } } } ``` 这段代码实现快速排序算法。在`quickSort`方法中,我们通过递归调用将数组切分成子数组,并对每个子数组进行排序。`partition`方法用于选择一个基准元素,并将小于该元素的元素放在基准元素的左侧,大于该元素的元素放在基准元素的右侧。最后,我们通过交换元素的位置来完成排序。 在`main`方法中,我们创建了一个测试数组,并依次打印原始数组排序后的数组。 ### 回答3: 快速排序是一种常用的排序算法,其基本思想是通过一趟排序将待排序的序列分割成独立的两部分,其中一部分小于基准元素,另一部分大于基准元素,然后再分别对这两部分继续进行排序,最终得到有序序列。 以下是使用Java实现快速排序的代码: ```java public class QuickSort { public static void quickSort(int[] arr, int left, int right) { if (left < right) { int pivotIndex = partition(arr, left, right); quickSort(arr, left, pivotIndex - 1); quickSort(arr, pivotIndex + 1, right); } } public static int partition(int[] arr, int left, int right) { int pivotValue = arr[left]; // 选取第一个元素作为基准元素 int i = left, j = right; while (i < j) { // 从右往左找第一个小于基准元素的值 while (i < j && arr[j] >= pivotValue) { j--; } if (i < j) { arr[i++] = arr[j]; } // 从左往右找第一个大于基准元素的值 while (i < j && arr[i] <= pivotValue) { i++; } if (i < j) { arr[j--] = arr[i]; } } arr[i] = pivotValue; return i; } public static void main(String[] args) { int[] arr = {5, 2, 8, 9, 1, 3}; quickSort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } } ``` 以上代码中,`quickSort`方法用于递归调用,将数组按照基准元素分成两部分分别进行排序,`partition`方法用于确定基准元素的位置并调整数组顺序。在`main`方法中,我们可以看到对数组进行快速排序的调用,并打印排序后的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值