#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
using namespace std;
class solution{
public:
int randominrange(int a, int b)
{
srand(NULL);//随机数的产生,需要种子
int num = a + rand() % (b - a + 1);
return num;
}
int partition(int* data, int left, int right)
{
int small;
int index = randominrange(left, right);
swap(data[index], data[right]);//
small = left - 1;
for (int i = left; i < right; i++)
{
if (data[i] < data[right])
{
small++;
if (i != small)
{
swap(data[small], data[i]);
}
}
}
small++;
swap(data[small], data[right]);
return small;
}
void quicksort(int* data,int left,int right)
{
if (right-left==0)//终止情况
{
}
if (right - left >= 1)//前向发展
{
int index = partition(data,left,right);
quicksort(data, left, index - 1);
quicksort(data, index + 1, right);
}
}
};
int main()
{
solution s;
const int len = 10;
vector<int> nums;
int nums1[len];
for (int i = 0; i < 10; i++)
{
nums.push_back(i);
}
int j = 0;
for (int i = len; i>0; i--)
{
srand(unsigned (time(NULL)));//每次产生不同的随机数
int rand1=rand() % i;
nums1[j]=nums[rand1];///产生一个范围内的随机序列,对有序数组产生随机索引,进而产生随机数组
nums.erase(nums.begin() + rand1);
j++;
}
for (int i = 0; i < len; i++)
{
cout << nums1[i] << ' ';
}
cout << endl;
s.quicksort(nums1,0,len-1);
for (int i = 0; i < len; i++)
{
cout << nums1[i] << ' ';
}
cout << endl;
}
#include<algorithm>
#include<ctime>
#include<vector>
using namespace std;
class solution{
public:
int randominrange(int a, int b)
{
srand(NULL);//随机数的产生,需要种子
int num = a + rand() % (b - a + 1);
return num;
}
int partition(int* data, int left, int right)
{
int small;
int index = randominrange(left, right);
swap(data[index], data[right]);//
small = left - 1;
for (int i = left; i < right; i++)
{
if (data[i] < data[right])
{
small++;
if (i != small)
{
swap(data[small], data[i]);
}
}
}
small++;
swap(data[small], data[right]);
return small;
}
void quicksort(int* data,int left,int right)
{
if (right-left==0)//终止情况
{
}
if (right - left >= 1)//前向发展
{
int index = partition(data,left,right);
quicksort(data, left, index - 1);
quicksort(data, index + 1, right);
}
}
};
int main()
{
solution s;
const int len = 10;
vector<int> nums;
int nums1[len];
for (int i = 0; i < 10; i++)
{
nums.push_back(i);
}
int j = 0;
for (int i = len; i>0; i--)
{
srand(unsigned (time(NULL)));//每次产生不同的随机数
int rand1=rand() % i;
nums1[j]=nums[rand1];///产生一个范围内的随机序列,对有序数组产生随机索引,进而产生随机数组
nums.erase(nums.begin() + rand1);
j++;
}
for (int i = 0; i < len; i++)
{
cout << nums1[i] << ' ';
}
cout << endl;
s.quicksort(nums1,0,len-1);
for (int i = 0; i < len; i++)
{
cout << nums1[i] << ' ';
}
cout << endl;
}