1.基础冒泡排序核心代码:
bool bubble(int* arr,int n){
bool changed = false;
for(int i=1;i<n;i++){
if(arr[i]<arr[i-1]){
int t = arr[i];
arr[i] = arr[i-1];
arr[i-1] = t;
changed = true;
}
}
return changed;
}
void bubble_sort(int* arr,int n){
for(int i=0;i<n-1;i++){
if(!bubble(arr,n-i)){
break;
}
}
}
1.1测试代码:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
int* createRandArray(int n){
int* arr = malloc(n*sizeof(int));
srand(time(NULL));
for(int i=0;i<n;i++){
arr[i]=rand()%100+1;
}
return arr;
}
void printArray(int* arr,int n){
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
printf("\n");
}
bool isOrder(int* arr,int n){
for(int i=1;i<n;i++){
if(arr[i]>=arr[i-1]){
continue;
}else{
return false;
}
}
return true;
}
bool bubble(int* arr,int n){
bool changed = false;
for(int i=1;i<n;i++){
if(arr[i]<arr[i-1]){
int t = arr[i];
arr[i] = arr[i-1];
arr[i-1] = t;
changed = true;
}
}
return changed;
}
void bubble_sort(int* arr,int n){
for(int i=0;i<n-1;i++){
if(!bubble(arr,n-i)){
break;
}
}
}
int main(){
int n;
scanf("%d",&n);
int* arr = createRandArray(n);
printArray(arr,n);
printf("Original is order?%s\n\n",isOrder(arr,n)?"yes":"no");
bubble_sort(arr,n);
printArray(arr,n);
printf("Bubble is order?%s\n\n",isOrder(arr,n)?"yes":"no");
free(arr);
arr= NULL;
}
2.类型通用版冒泡排序核心代码:
void memswp(void* a,void* b,int size){
unsigned char t[size];
memcpy(t,a,size);
memcpy(a,b,size);
memcpy(b,t,size);
}
typedef int(*cmp_t)(const void* a,const void* b);
bool bubble(void* arr,int n,int size,cmp_t cmp){
bool changed = false;
for(int i =1;i<n;i++){
if(cmp(arr+i*size,arr+(i-1)*size)<0){
unsigned char t[size];
memswp(arr+i*size,arr+(i-1)*size,size);
changed = true;
}
}
return changed;
}
void bsort(void* arr,int n,int size,cmp_t cmp){
for(int i=0;i<n-1;i++){
if(!bubble(arr,n-i,size,cmp)){
break;
}
}
}
2.1测试代码:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
int* createRandArray(int n){
int* arr = malloc(n*sizeof(int));
srand(time(NULL));
for(int i=0;i<n;i++){
arr[i]=rand()%100+1;
}
return arr;
}
void printArray(int* arr,int n){
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
printf("\n");
}
bool isOrder(int* arr,int n){
for(int i=1;i<n;i++){
if(arr[i]>=arr[i-1]){
continue;
}else{
return false;
}
}
return true;
}
void memswp(void* a,void* b,int size){
unsigned char t[size];
memcpy(t,a,size);
memcpy(a,b,size);
memcpy(b,t,size);
}
typedef int(*cmp_t)(const void* a,const void* b);
bool bubble(void* arr,int n,int size,cmp_t cmp){
bool changed = false;
for(int i =1;i<n;i++){
if(cmp(arr+i*size,arr+(i-1)*size)<0){
unsigned char t[size];
memswp(arr+i*size,arr+(i-1)*size,size);
changed = true;
}
}
return changed;
}
void bsort(void* arr,int n,int size,cmp_t cmp){
for(int i=0;i<n-1;i++){
if(!bubble(arr,n-i,size,cmp)){
break;
}
}
}
int cmpint(const void* a,const void* b){
return *(int*)a - *(int*)b;
}
int cmpchar(const void* a,const void* b){
return *(char*)a - *(char*)b;
typedef struct{
char name[10];
int score;
} Test;
int cmpTest(const void* a,const void* b){
return ((Test*)a)->score - ((Test*)b)->score;
}
int main(){
int n;
scanf("%d",&n);
int* arr = createRandArray(n);
printArray(arr,n);
printf("Original is order?%s\n\n",isOrder(arr,n)?"yes":"no");
bsort(arr,n,sizeof(int),cmpint);
printArray(arr,n);
printf("bsort is order?%s\n\n",isOrder(arr,n)?"yes":"no");
char s[]="123azHello";
bsort(s,strlen(s),sizeof(char),cmpchar);
printf("%s\n",s);
free(arr);
arr= NULL;
}