自己整理的八种排序(c/c++)-创新互联
//1.直接插入排序
void InsertSort(int A[],int n){
int i,j;
for(i = 2;i< n;i++){
if(A[i-1] >A[i]){
A[0] = A[i];
for(j = i-1; jA[j] >A[0];--){
A[j+1] = A[j];
}
A[j+1] = A[0];
}
}
}
1.1折半插入排序
void InsertSort(int A[],int n){
int i,j,left,right,mid;
for(i = 2;i< n; i++){
A[0] = A[i];
left = 1,right = i - 1;
while(left<= right){
mid = (left + right)/2;
if(A[mid] >A[0]) right = mid - 1;
else left = mid + 1;
}
for(j = i - 1;j >= right + 1;j--){
A[j + 1] = A[j];
}
A[j + 1] = A[0];
}
}
2.冒泡排序
void BubbleSort(int A[],int n){
int i,j;
for(i = 0;i< n - 1; i++){
bool flag = false;
for(j = n - 1;j >i; j--){
if(A[j - 1] >A[j]){
swap(A[j - 1],A[j]);
flag = true;
}
}
3.快速排序
int MAXN = 1000;
int arr[MAXN];
void Partition(int left,int right){
while(left< right){
while(left< right && arr[left]< arr[right]){
right --;
}
swap(arr[left],arr[right]);
while(left< right && arr[left]< arr[right]){
left++;
}
swap(arr[left],arr[right]);
}
return left;
}
void QuickSort(int left,int right){
if(left< right){
int pos = Partition(left,right);
QuickSort(left, pos - 1);
QuickSort(pos + 1, right);
}
}
4.简单选择排序
void SelectSort(int A[],int n){
int i,j,min;
for(i = 0; i< n - 1; i++){
min = i;
for(j = i + 1;j< n; j++){
if(A[min] >A[j]){
min = j;
}
if(min != i)
swap(A[min],A[i]);
}
}
}
if(flag == false) return;
}
}
//5.归并排序
int arr[MAXN];
int temp[MAXN];
void Combine(int left,int mid,int right){
int i = left;
int k = left;
int j = mid + 1;
while(i<= mid && j<= right){
if(arr[i]< arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while(i<= mid) temp[k++] = arr[i++];
while(j<= right) temp[k++] = arr[j++];
for(k = left; k<= right; k++){
arr[k] = temp[k];
}
}
void MergeSort(int left,int right){
if(left< right){
int mid = (left + right)/2;
MergeSort(left,mid);
MergeSort(mid + 1,right);
Combine(left,mid,right);
}
}
//6.快速排序
#include#include#include //random函数
#include //交换函数
using namespace std;
const int MAXN = 1000 + 10;
int arr[MAXN];
Partition(int arr[],int left, int right){
while(left< right){
while(left< right && arr[left]<= arr[right]){
right--;
}
swap(arr[left],arr[right]);
while(left< right && arr[left]<= arr[right]){
left++;
}
swap(arr[left],arr[right]);
}
return left;
}
void QuickSort(int arr[],int left, int right){
if(left< right){
int position = Partition(arr,left, right);
QuickSort(arr,left, position - 1);
QuickSort(arr,position + 1,right);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0; i#includeusing namespace std;
const int MAXN = 1000 + 10;
int arr[MAXN];
int temp[MAXN];
void Combine(int arr[],int temp[],int left, int middle, int right){
int i = left;
int j = middle + 1;
int k = left;
while(i<=middle && j<= right){
if(arr[i]<= arr[j]){
temp[k++] = arr[i++];
}else{
temp[k++] = arr[j++];
}
}
while(i<= middle){
temp[k++] = arr[i++];
}
while(j<=right){
temp[k++] = arr[j++];
}
for(k = left; k<= right; k++){
arr[k] = temp[k];
}
}
void MergeSort(int arr[],int left, int right){
if(left< right){
int middle = left +(right - left)/2;
MergeSort(arr,left,middle);
MergeSort(arr,middle+1, right);
Combine(arr,temp,left,middle,right);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0; i
//1.直接插入排序
void InsertSort(int A[],int n){
int i,j;
for(i = 2;i< n;i++){
if(A[i-1] >A[i]){
A[0] = A[i];
for(j = i-1; jA[j] >A[0];--){
A[j+1] = A[j];
}
A[j+1] = A[0];
}
}
}
1.1折半插入排序
void InsertSort(int A[],int n){
int i,j,left,right,mid;
for(i = 2;i< n; i++){
A[0] = A[i];
left = 1,right = i - 1;
while(left<= right){
mid = (left + right)/2;
if(A[mid] >A[0]) right = mid - 1;
else left = mid + 1;
}
for(j = i - 1;j >= right + 1;j--){
A[j + 1] = A[j];
}
A[j + 1] = A[0];
}
}
2.冒泡排序
void BubbleSort(int A[],int n){
int i,j;
for(i = 0;i< n - 1; i++){
bool flag = false;
for(j = n - 1;j >i; j--){
if(A[j - 1] >A[j]){
swap(A[j - 1],A[j]);
flag = true;
}
}
3.快速排序
int MAXN = 1000;
int arr[MAXN];
void Partition(int left,int right){
while(left< right){
while(left< right && arr[left]< arr[right]){
right --;
}
swap(arr[left],arr[right]);
while(left< right && arr[left]< arr[right]){
left++;
}
swap(arr[left],arr[right]);
}
return left;
}
void QuickSort(int left,int right){
if(left< right){
int pos = Partition(left,right);
QuickSort(left, pos - 1);
QuickSort(pos + 1, right);
}
}
4.简单选择排序
void SelectSort(int A[],int n){
int i,j,min;
for(i = 0; i< n - 1; i++){
min = i;
for(j = i + 1;j< n; j++){
if(A[min] >A[j]){
min = j;
}
if(min != i)
swap(A[min],A[i]);
}
}
}
if(flag == false) return;
}
}
//5.归并排序
int arr[MAXN];
int temp[MAXN];
void Combine(int left,int mid,int right){
int i = left;
int k = left;
int j = mid + 1;
while(i<= mid && j<= right){
if(arr[i]< arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while(i<= mid) temp[k++] = arr[i++];
while(j<= right) temp[k++] = arr[j++];
for(k = left; k<= right; k++){
arr[k] = temp[k];
}
}
void MergeSort(int left,int right){
if(left< right){
int mid = (left + right)/2;
MergeSort(left,mid);
MergeSort(mid + 1,right);
Combine(left,mid,right);
}
}
//6.快速排序
#include#include#include //random函数
#include //交换函数
using namespace std;
const int MAXN = 1000 + 10;
int arr[MAXN];
Partition(int arr[],int left, int right){
while(left< right){
while(left< right && arr[left]<= arr[right]){
right--;
}
swap(arr[left],arr[right]);
while(left< right && arr[left]<= arr[right]){
left++;
}
swap(arr[left],arr[right]);
}
return left;
}
void QuickSort(int arr[],int left, int right){
if(left< right){
int position = Partition(arr,left, right);
QuickSort(arr,left, position - 1);
QuickSort(arr,position + 1,right);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0; i#includeusing namespace std;
const int MAXN = 1000 + 10;
int arr[MAXN];
int temp[MAXN];
void Combine(int arr[],int temp[],int left, int middle, int right){
int i = left;
int j = middle + 1;
int k = left;
while(i<=middle && j<= right){
if(arr[i]<= arr[j]){
temp[k++] = arr[i++];
}else{
temp[k++] = arr[j++];
}
}
while(i<= middle){
temp[k++] = arr[i++];
}
while(j<=right){
temp[k++] = arr[j++];
}
for(k = left; k<= right; k++){
arr[k] = temp[k];
}
}
void MergeSort(int arr[],int left, int right){
if(left< right){
int middle = left +(right - left)/2;
MergeSort(arr,left,middle);
MergeSort(arr,middle+1, right);
Combine(arr,temp,left,middle,right);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0; i
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
我们提供的服务有:成都网站制作、网站设计、微信公众号开发、网站优化、网站认证、和县ssl等。为上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的和县网站制作公司当前文章:自己整理的八种排序(c/c++)-创新互联
本文路径:http://myzitong.com/article/dichjs.html