请用C++实现冒泡排序、快速排序、斐波那契数列
时间: 2023-09-22 08:06:51 浏览: 117
### 回答1:
好的,这是实现冒泡排序、快速排序和斐波那契数列的C语言代码:
冒泡排序:
```
#include<stdio.h>
#define MAX 100
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main()
{
int arr[MAX], n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
printf("Sorted array: ");
for(i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
```
快速排序:
```
#include<stdio.h>
#define MAX 100
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
int j, temp;
for(j=low; j<=high-1; j++)
{
if(arr[j] <= pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
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[MAX], n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n-1);
printf("Sorted array: ");
for(i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
return
### 回答2:
冒泡排序是一种简单的排序算法,可以按照从小到大或者从大到小的顺序将一个数组排序。算法的基本思想是将相邻的元素进行比较,如果顺序不对则进行交换,每一轮遍历可以确保一个元素位于正确的位置上。重复这个过程直至整个数组排序完成。以下是用C语言实现冒泡排序的代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int 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("排序后的数组:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
快速排序是一种常用的排序算法,其基本原理是选择一个基准元素,将比基准元素小的放在左边,比基准元素大的放在右边,然后对左右两个子序列进行递归排序。以下是用C语言实现快速排序的代码:
```c
#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
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[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("排序后的数组:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
斐波那契数列是一个数列,其中每个数字都是前两个数字的和。以下是用C语言实现斐波那契数列的代码:
```c
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n = 10;
printf("斐波那契数列前 %d 项:", n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
```
### 回答3:
冒泡排序是一种简单的排序算法,它通过比较和交换相邻元素来逐渐将最大(或最小)的元素冒泡到数组的末尾。以下是使用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("排序后的数组:");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
```
快速排序是一种基于分治法的排序算法,通过选择一个基准元素,将数组划分为两个子数组,并对子数组分别进行排序。以下是使用C语言实现快速排序的代码示例:
```c
#include <stdio.h>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
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[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("排序后的数组:");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
```
斐波那契数列是一种常见的数列,其中每个数字都是前两个数字的和。以下是使用C语言实现斐波那契数列的代码示例:
```c
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n = 10;
printf("斐波那契数列前 %d 项:", n);
for (int i = 0; i < n; i++)
printf("%d ", fibonacci(i));
return 0;
}
```
以上便是分别使用C语言实现冒泡排序、快速排序和斐波那契数列的代码示例。
阅读全文