快速排序
C实现
void fastStore(int *a, int start, int end){
if(start>=end)
return ;
int left=start;
int right=end;
int temp=a[left];//设置基准值temp
while(left < right) //左指针的位置一定小于右指针的位置
{
while(a[right]>temp && left < right)
//左指针要在右指针的左面
{
right--;
}
a[left] = a[right];
left++;
while(a[left] < temp && left < right){
left++;
}
a[right] = a[left];
right--;
a[left] = temp;
}
fastSort(a,start,left-1);
fastSort(a,right+1,end);
}
void FastSort(int *a,int start,int end)
{
if(start>=end)
{
return;
}
int left = start;
int right = end;
int temp = a[left];
while(left < right)
{
while(a[right]>temp && left < right)
{
right--;
}
a[left] = a[right];
left++;
while(a[left]<temp && left<right)
{
left++;
}
a[right] = a[left];
right--;
a[left] = temp;
}
FastSort(a,start, left-1);
FastSort(a,right+1, end);
}
C++实现
#include<iostream>
//快排具体实现
template<typename T>
void showNum(T *num,int len)
{
for (int j = 0; j < len; j++)
{
std::cout<<num[j]<<" ";
}
std::cout<<std::endl;
}
template<typename T>
int part(T* arr, int left, int right) //划分函数
{
int i = left;
int j = right;
T fidValue = arr[left];
while (i < j)
{
while (i<j && arr[j]>fidValue) //从右向左开始找一个 小于等于 pivot的数值
{
j--;
}
if (i < j)
{
std::swap(arr[i++], arr[j]); //r[i]和r[j]交换后 i 向右移动一位
}
while (i < j && arr[i] <= fidValue) //从左向右开始找一个 大于 pivot的数值
{
i++;
}
if (i < j)
{
std::swap(arr[i], arr[j--]); //r[i]和r[j]交换后 i 向左移动一位
}
}
return i; //返回最终划分完成后基准元素所在的位置
}
template<typename T>
void fastSort(T *arr,int left,int right)
{
int mid;
if (left < right)
{
mid = part(arr, left, right); // 返回基准元素位置
fastSort(arr, left, mid - 1); // 左区间递归快速排序
fastSort(arr, mid+1, right); // 右区间递归快速排序
}
}
template<typename T>
void Sort(T *num,int len)
{
fastSort(num,0,len-1);
}
int main()
{
int num[] = {4,2,1,6,3,8,12,0,34};
Sort<int>(num,sizeof(num)/sizeof(num[0]));
showNum<int>(num,sizeof(num)/sizeof(num[0]));
std::string str[]={"w ","c ","d ","a "};
Sort<std::string>(str,sizeof(str)/sizeof(str[0]));
showNum<std::string>(str,sizeof(str)/sizeof(str[0]));
return 0;
}
插入排序
#include<stdio.h>
void ShowArray(int *num, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ",num[i]);
}
printf("\n");
}
void InsertSort(int *num, int len)
{
for(int i=0;i<len;i++)
{
int temp=num[i];
int j=i;
for(;j>0;j--){
if(temp<num[j-1]){
num[j]=num[j-1];
}
else{
break;
}
}
num[j]=temp;
}
}
int main()
{
int num[]={2,7,1,4,8,3,9};
InsertSort(num,sizeof(num)/sizeof(num[0])-1);
ShowArray(num,sizeof(num)/sizeof(num[0]));
return 0;
}
冒泡排序
#include<stdio.h>
void ShowArray(int *num, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ",num[i]);
}
printf("\n");
}
void BubbleSort(int *num,int size)
{
for (int i = 0; i < size-1; i++)
{
for (int j = 0; j <size-i-1; j++)
{
if (num[j]>num[j+1])
{
int tmp = num[j];
num[j]=num[j+1];
num[j+1] = tmp;
}
}
}
}
int main()
{
int num[]={2,7,1,4,8,3,9};
BubbleSort(num,sizeof(num)/sizeof(num[0]));
ShowArray(num,sizeof(num)/sizeof(num[0]));
return 0;
}
选择排序
#include<stdio.h>
void ShowArray(int *num, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ",num[i]);
}
printf("\n");
}
void ChooseSort(int *num, int len)
{
int minnum = 0;
for(int i=0;i<len;i++)
{
minnum = num[i];
for (int j = i+1; j < len; j++)
{
if (num[j]<minnum)
{
int temp = minnum;
minnum = num[j];
num[j] = temp;
}
}
num[i]=minnum;
}
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void ChooseSort2(int *num , int len)
{
int i, j, minnum;
for(i = 0; i < len; i++)
{
minnum = i;
for(j = i+1; j<len; j++)
{
if (num[j]<num[minnum])
minnum = j;
}
swap(&num[minnum],&num[i]);
}
}
int main()
{
int num[]={2,7,1,4,8,3,9};
ChooseSort2(num,sizeof(num)/sizeof(num[0])-1);
ShowArray(num,sizeof(num)/sizeof(num[0]));
return 0;
}