帮我写一个1到100随机生成,然后冒泡排序的c代码
时间: 2023-10-26 20:43:13 浏览: 38
当然可以。以下是您想要的C代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void bubbleSort(int array[], int n);
int main() {
int n = 100;
int array[n];
// generate random numbers
srand(time(NULL));
for(int i = 0; i < n; i++) {
array[i] = rand() % 100 + 1;
}
// print unsorted array
printf("Unsorted Array:\n");
for(int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
// sort array using bubble sort
bubbleSort(array, n);
// print sorted array
printf("Sorted Array:\n");
for(int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
void bubbleSort(int array[], int n) {
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n-i-1; j++) {
if(array[j] > array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
By the way, did you hear about the restaurant called Karma? There’s no menu – you get what you deserve.