写一个给整数排序的C语言代码
时间: 2024-05-16 18:16:33 浏览: 12
好的,以下是一个简单的 C 语言代码,用于从用户输入的整数数组中进行排序:
```c
#include <stdio.h>
#define MAX_SIZE 100 // 定义整数数组最大长度
int main() {
int array[MAX_SIZE];
int n, i, j, temp;
printf("请输入整数数组长度:");
scanf("%d", &n);
printf("请输入 %d 个整数:", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
// 冒泡排序
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("排序后的整数数组为:");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
return 0;
}
```
该代码使用冒泡排序算法,该算法的时间复杂度为 O(n²)。如果需要更高效的排序算法,可以使用快速排序、归并排序等。
相关问题
给我写一个冒泡排序的C语言代码
以下是冒泡排序的C语言代码:
```
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
该代码定义了一个 `bubbleSort()` 函数来实现冒泡排序。在 `main()` 函数中,我们定义了一个整数数组并将其传递给 `bubbleSort()` 函数。然后,我们打印排序后的数组。
帮我写一个快速排序的C语言代码
### 回答1:
好的,以下是快速排序的 C 代码实现:
```c
#include <stdio.h>
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = { 10, 7, 8, 9, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
该代码实现了快速排序算法,时间复杂度为 O(nlogn)。
### 回答2:
当然可以!下面是一个简单的C语言快速排序算法的代码示例:
```c
#include <stdio.h>
// 交换两个元素的位置
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// 将pivot放到正确的位置,并返回其索引
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // 选取最后一个元素作为pivot
int i = low - 1; // 最小元素的索引
for (int j = low; j <= high - 1; j++) {
// 如果当前元素小于等于pivot,则交换它与较小元素的位置
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
// 交换pivot和较小元素的位置
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
// 快速排序函数
void quickSort(int arr[], int low, int high) {
if (low < high) {
// 划分数组,并获取pivot的索引
int pivotIndex = partition(arr, low, high);
// 递归排序pivot左边的子数组
quickSort(arr, low, pivotIndex - 1);
// 递归排序pivot右边的子数组
quickSort(arr, pivotIndex + 1, high);
}
}
// 测试快速排序算法
int main() {
int arr[] = {64, 7, 89, 12, 37, 31, 9};
int n = sizeof(arr) / sizeof(arr[0]);
printf("排序前的数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
quickSort(arr, 0, n - 1);
printf("\n排序后的数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
以上代码实现了一个简单的快速排序算法。该算法首先选择最后一个元素作为pivot,然后通过划分数组将比pivot小的元素放在其左边,大的放在右边,然后递归地对两边的子数组进行排序。最后,打印出排序前后的数组以进行验证。
您可以根据自己的需求和细节对代码进行调整和改进。希望能帮到您!
### 回答3:
快速排序是一种常用的高效排序算法。它的基本思想是选择一个基准数,通过一趟排序将数组分为两个部分,其中一部分的所有数都比基准数小,另一部分的所有数都比基准数大,然后递归地将两部分进行排序,最终得到有序的数组。以下是用C语言实现快速排序的代码:
```c
#include <stdio.h>
void qsort(int arr[], int left, int right){
int i, j, temp, pivot;
if(left < right){
pivot = left; //将基准数设置为最左边的数
i = left;
j = right;
while(i < j){
while(arr[i] <= arr[pivot] && i < right) //从左侧找到比基准数大的数
i++;
while(arr[j] > arr[pivot]) //从右侧找到比基准数小的数
j--;
if(i < j){ //交换找到的两个数
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[pivot]; //将基准数放到中间位置
arr[pivot] = arr[j];
arr[j] = temp;
qsort(arr, left, j-1); //递归排序基准数左边的部分
qsort(arr, j+1, right); //递归排序基准数右边的部分
}
}
int main() {
int arr[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int len = sizeof(arr) / sizeof(arr[0]);
qsort(arr, 0, len-1);
printf("排序后的数组:");
for(int i=0; i<len; i++){
printf("%d ", arr[i]);
}
return 0;
}
```
以上是一个使用C语言实现的快速排序算法。这段代码首先定义了一个`qsort()`函数,用于进行快速排序。在`main()`函数中,我们声明了一个整数数组`arr`作为排序示例,然后调用`qsort()`函数对数组进行排序。最后输出排序后的数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)