#include "iostream"
using namespace std;
class A
{
private:
void merge(int arr[], int left, int middle, int right)
{
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;
int* L = new int[n1];
int* R = new int[n2];
for (i = 0; i < n1; i++)
L[i] = arr[left+i];
for (j = 0; j < n2; j++)
R[j] = arr[middle+1+j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (L[i] < R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)arr[k++] = L[i++];
while (j < n2)arr[k++] = R[j++];
}
public:
void quick_sort(int arr[], int left, int right)
{
if (left >= right)return;
int i = left, j = right, temp = arr[left];
while (i < j)
{
while (arr[j] >= temp && i < j)j--;
while (arr[i] <= temp && i < j)i++;
if (i < j)
{
int k = arr[i];
arr[i] = arr[j];
arr[j] = k;
}
}
arr[left] = arr[i];
arr[i] = temp;
quick_sort(arr,left, i - 1);
quick_sort(arr,i + 1, right);
}
void merge_sort(int arr[], int left, int right)
{
if (left >= right)return;
int middle = (right + left) / 2;
merge_sort(arr, left, middle);
merge_sort(arr, middle + 1, right);
merge(arr, left, middle, right);
}
void pop_sort(int arr[], int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
};
int main(void)
{
int arr[10] = { 8,12,24,4,9,6,4,2,1,3 };
A var;
var.pop_sort(arr,10);
for (int i = 0; i < 10; i++)
cout << arr[i] << " ";
cout << endl;
}
归并,快排,冒泡排序,c++合集
最新推荐文章于 2024-11-05 21:58:11 发布