不要逼逼赖赖,有本事键盘上敲一敲
好了,直接上代码!!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <malloc.h>
void bubbleSort(int *arr,int n){
for(int i=0; i<n; i++){
int flag = false;
for(int j=0; j<n-i-1; j++){
if(arr[j]>arr[j+1]){
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
flag = true;
}
}
if(flag==false) return;
}
}
int findMax(int *arr,int n){
int max = arr[0];
int pos = 0;
for(int i=0; i<n; i++){
if(arr[i]>max){
max = arr[i];
pos = i;
}
}
return pos;
}
void selectSort(int *arr,int n){
if(n>0){
int pos = findMax(arr,n);
int temp = arr[n-1];
arr[n-1] = arr[pos];
arr[pos] = temp;
n--;
selectSort(arr,n);
}
}
void insert(int *arr, int n){
int key = arr[n-1];
int i = n-1;
while(arr[i-1]>key){
arr[i] = arr[i-1];
i--;
if(i==0) break;
}
arr[i] = key;
}
void insertSort(int *arr,int n){
if(n==0 || n==1) return;
for(int i=1; i<n; i++){
insert(arr,i);
}
}
void Merge(int *A,int low,int mid, int high){
int *B = (int*)malloc((high+1)* sizeof(int));
for(int i=low; i<=high; i++){
B[i] = A[i];
}
int i,j,k;
for(i=low, j=mid+1, k=i; i<=mid&&j<=high; k++){
if(B[i]<=A[j]){
A[k] = B[i++];
}
else
{
A[k] = B[j++];
}
}
while(i<=mid){
A[k++] = B[i++];
}
while(j<=high){
A[k++] = B[j++];
}
}
void MergeSort(int *arr,int low,int high){
if(low<high){
int mid = (low+high)/2;
MergeSort(arr,low,mid);
MergeSort(arr,mid+1,high);
Merge(arr,low,mid+1,high);
}
}
void quickSort(int *arr,int start,int end){
int temp = arr[start];
int i = start;
int j = end;
while(i<j){
while(i<j && arr[j]>temp){
j--;
}
if(i<j){
arr[i++] = arr[j];
}
while(i<j && arr[i]<=temp){
i++;
}
if(i<j){
arr[j--] = arr[i];
}
arr[i] = temp;
quickSort(arr,start,i-1);
quickSort(arr,i+1,end);
}
}
void shellSort(int *arr,int n){
int dk,i,j;
for(dk=n/2; dk>=1; dk=dk/2){
for(i=dk+1; i<n; i++){
if(arr[i]<arr[i-dk]){
int temp = arr[i];
for(j=i; j>0&&arr[j]>arr[j-dk]; j=j-dk){
arr[j] = arr[j-dk];
}
arr[j] = temp;
}
}
}
}
void swap(int *arr, int pos){
int left = pos*2;
int right = pos*2+1;
if(arr[pos]<arr[left]){
int temp = arr[pos];
arr[pos] = arr[left];
arr[left] = temp;
}
if(arr[pos]<arr[right]){
int temp = arr[pos];
arr[pos] = arr[right];
arr[right] = temp;
}
}
void adopt(int *arr,int n){
for(int i=(n-1)/2; i>0; i--){
swap(arr,i);
}
}
void heapSort(int *arr,int n){
for(int i=n-1; i>0; i--){
adopt(arr,i);
int temp = arr[1];
arr[1] = arr[i];
arr[i] = temp;
}
}
int main(){
int arr[] = {4,1,2,0,6,9,32,8};
int start = 0;
int end = 7;
// shellSort(arr,8);
// bubbleSort(arr,end+1);
// selectSort(arr,end+1);
// insertSort(arr,end+1);
// MergeSort(arr,start,end);
// quickSort(arr,start,end);
// heapSort(arr,end+1);
for(int i=0; i<end+1; i++){
printf("%d ",arr[i]);
}
return 0;
}