#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int parition(vector<int>& vt,int left,int right)
{
int i=left,j=right;
int pivot=vt[left];
while(i<j)
{
while (i<j && pivot <=vt[j])
{
j--;
}
vt[i]=vt[j];
while(i<j && pivot >= vt[i])
{
i++;
}
vt[j]=vt[i];
}
vt[i]=pivot;
return i;
}
void quickSort(vector<int>& vt,int left,int right)
{
if(left >= right)
return ;
int pivot=parition(vt,left,right);
quickSort(vt,left,pivot-1);
quickSort(vt,pivot+1,right);
}
void quickSort(vector<int>& vt)
{
quickSort(vt,0,vt.size()-1);
}
void bulleSort(vector<int>& vt)
{
for(int i=0;i<vt.size();i++)
{
bool isSorted=true;
for(int j=i+1;j<vt.size();j++)
{
if(vt[i]>vt[j])//前后互相交换
{
swap(vt[i],vt[j]);
isSorted=false;
}
}
if(isSorted)
{
break;
}
}
}
void bulleSort002(vector<int>& vt)
{
for(int i=0;i<vt.size();i++)
{
bool isSorted=true;
for(int j=0;j<vt.size()-i-1;j++)
{
if(vt[j]>vt[j+1])//前后互相交换
{
swap(vt[j],vt[j+1]);
isSorted=false;
}
}
if(isSorted)
{
break;
}
}
}
int main() {
std::cout << "Hello, World!" << std::endl;
//cout<<std::hex<<1024<<endl;
{
vector<int> vt={32,9,5,6,7,5,1,3,4,16,14};
quickSort(vt);
std::copy(vt.begin(), vt.end(),std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl;
}
{
vector<int> vt={33,9,5,6,7,5,1,3,4,16,14};
bulleSort(vt);
std::copy(vt.begin(), vt.end(),std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl;
}
{
vector<int> vt={36,9,5,6,7,5,1,3,4,16,14};
bulleSort002(vt);
std::copy(vt.begin(), vt.end(),std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl;
}
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
//https://baike.baidu.com/item/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95
void Qsort(int arr[], int low, int high){
if (high <= low) return;
int i = low;
int j = high + 1;
int key = arr[low];
while (true)
{
/*从左向右找比key大的值*/
while (arr[++i] < key)
{
if (i == high){
break;
}
}
/*从右向左找比key小的值*/
while (arr[--j] > key)
{
if (j == low){
break;
}
}
if (i >= j) break;
/*交换i,j对应的值*/
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*中枢值与j对应值交换*/
int temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
Qsort(arr, low, j - 1);
Qsort(arr, j + 1, high);
}
void swap(int* a,int i,int j)
{
int temp=a[i];
a[j]=a[i];
a[i]=temp;
}
//https://blog.csdn.net/earbao/article/details/50417644
void quick_sort002(int* a,int left,int right)
{
if(left >= right)
return ;
int i=left;
int j=right;
int key=a[left];
while(i<j)
{
while(i<j && key <=a[j])
{
j--;
}
a[i]=a[j];
while(i<j && key >= a[i])
{
i++;
}
a[j]=a[i];
}
a[i]=key;
quick_sort002(a,left,i-1);
quick_sort002(a,i+1,right);
}
int parition(int *a, int low, int high) {
int left=low;
int right=high;
int pivot=a[low];
while(left < right)//基数
{
while(left <right && pivot <=a[right])//找到比基数小的元素
{
right--;
}
a[left]=a[right];
while(left<right && pivot >= a[left])//找到比基数大的元素
{
left++;
}
a[right]=a[left];
}
a[left]=pivot;
return left;
}
//https://blog.csdn.net/earbao/article/details/50417644
void quick_sort(int* a,int low,int high) {
if (low >= high)
return;
int pivot=parition(a,low,high);
quick_sort(a,low,pivot-1);
quick_sort(a,pivot+1,high);
}
#define NUM 16
#define MAX 100
void printData(const int *a);
//c++&java之快速排序算法
int main()
{
srand((unsigned)time(0));
{
int* a=new int[NUM];
for (int i = 0; i < NUM; ++i) {
a[i]=rand()% MAX;
}
printf("待排数列:\n");
printData(a);
Qsort(a, 0, NUM - 1);
printf("排序后数列:\n");
printData(a);
delete[] a;
}
{
int* a=new int[NUM];
for (int i = 0; i < NUM; ++i) {
a[i]=rand()% MAX;
}
printf("待排数列:\n");
printData(a);
quick_sort002(a, 0, NUM - 1);
printf("排序后数列:\n");
printData(a);
delete[] a;
}
{
int* a=new int[NUM];
for (int i = 0; i < NUM; ++i) {
a[i]=rand()% MAX;
}
printf("待排数列:\n");
printData(a);
quick_sort(a, 0, NUM - 1);
printf("排序后数列:\n");
printData(a);
delete[] a;
}
return 0;
}
void printData(const int *a) {
for(int i = 0; i < NUM; i++)
{
cout << a[i] << " ";
}
cout<<endl;
}
package com.yunshouhu.test_rsa;
import java.security.SecureRandom;
import java.util.Arrays;
public class test_quick_sort {
//https://www.sohu.com/a/246785807_684445
private static int partition(int[] arr, int startIndex, int endIndex) {
// 取第一个位置的元素作为基准元素
int pivot = arr[startIndex];
int left = startIndex;
int right = endIndex;
int index = startIndex;
//大循环在左右指针重合或者交错时结束
while (right >= left) {
while (right >= left) {
//right指针从右向左进行比较
if (arr[right] < pivot) {
arr[left] = arr[right];
index = right;
left++;
break;
}
right--;
}
while (right >= left) {
//left指针从左向右进行比较
if (arr[left] > pivot) {
arr[right] = arr[left];
index = left;
right--;
break;
}
left++;
}
}
arr[index] = pivot;
return index;
}
public static void quickSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
// 得到基准元素位置
int pivotIndex = partition(arr, left, right);
// 用分治法递归数列的两部分
quickSort(arr, left, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, right);
}
//https://baike.baidu.com/item/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95
public static int[] qsort(int arr[], int start, int end) {
int pivot = arr[start];
int i = start;
int j = end;
while (i < j) {
while ((i < j) && arr[j] > pivot) {
j--;
}
while ((i < j) && arr[i] < pivot) {
i++;
}
if (arr[i] == arr[j] && (i < j)) {
i++;
} else {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
if (i - 1 > start) {
arr = qsort(arr, start, i - 1);
}
if (j + 1 < end) {
arr = qsort(arr, j + 1, end);
}
return arr;
}
public static void main(String[] args) {
{
int arr[] = new int[]{12, 4, 7, 6, 5, 3, 2, 8, 1};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
{
int len=16;
int arr[] = new int[len];
byte[] data=new byte[len];
SecureRandom sr=new SecureRandom();
sr.nextBytes(data);
for (int i=0;i<data.length;i++) {
arr[i]=data[i];
}
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
{
int arr[] = new int[]{12, 4, 7, 6, 5, 3, 2, 8, 1};
qsort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
{
int arr[] = new int[]{3, 3, 3, 7, 9, 122344, 4656, 34, 34, 4656, 5, 6, 7, 8, 9, 343, 57765, 23, 12321};
int len = arr.length - 1;
arr = qsort(arr, 0, len);
System.out.println(Arrays.toString(arr));
}
}
}