三种最简单基础的排序 选择排序 冒泡排序 直接插入排序 运用了函数指针数组
来源:互联网 发布:矩阵各元素之和叫什么 编辑:程序博客网 时间:2024/05/01 09:54
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void CreateArray(int *a, int n);
void PrintArray(int *a, int n);
void BubbleSort(int *a, int n);
void SelectSort(int *a, int n);
void InsertSort(int *a, int n);
int main()
{
int a[10];
int select;
CreateArray(a, 10);
PrintArray(a, 10);
void (*p[3])(int *, int) = {BubbleSort, InsertSort, SelectSort};
printf("Which Sort do you prefer?\n");
printf("1 BubbleSort\n");
printf("2 InsertSort\n");
printf("3 SelectSort\n");
scanf("%d",&select);
(p[select - 1])(a, 10);
PrintArray(a, 10);
return 0;
}
void InsertSort(int *a, int n)
{
int temp;
for (int i = 1; i < n; ++i)
{
temp = a[i];
int j;
for (j = i - 1; j >= 0 && a[j] > temp; --j)
{
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
}
void SelectSort(int *a, int n)
{
int min;
int temp;
for (int i = 0; i < n - 1; ++i)
{
min = i;
for (int j = i + 1; j < n; ++j)
{
if (a[min] > a[j])
{
min = j;
}
}
if (min != i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
void BubbleSort(int *a, int n)
{
int temp;
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n - i - 1; ++j)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
void PrintArray(int *a, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
}
void CreateArray(int *a, int n)
{
srand(time(NULL));
for (int i = 0; i < n; ++i)
{
a[i] = rand()%100 + 1;
}
}
#include <stdlib.h>
#include <time.h>
void CreateArray(int *a, int n);
void PrintArray(int *a, int n);
void BubbleSort(int *a, int n);
void SelectSort(int *a, int n);
void InsertSort(int *a, int n);
int main()
{
int a[10];
int select;
CreateArray(a, 10);
PrintArray(a, 10);
void (*p[3])(int *, int) = {BubbleSort, InsertSort, SelectSort};
printf("Which Sort do you prefer?\n");
printf("1 BubbleSort\n");
printf("2 InsertSort\n");
printf("3 SelectSort\n");
scanf("%d",&select);
(p[select - 1])(a, 10);
PrintArray(a, 10);
return 0;
}
void InsertSort(int *a, int n)
{
int temp;
for (int i = 1; i < n; ++i)
{
temp = a[i];
int j;
for (j = i - 1; j >= 0 && a[j] > temp; --j)
{
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
}
void SelectSort(int *a, int n)
{
int min;
int temp;
for (int i = 0; i < n - 1; ++i)
{
min = i;
for (int j = i + 1; j < n; ++j)
{
if (a[min] > a[j])
{
min = j;
}
}
if (min != i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
void BubbleSort(int *a, int n)
{
int temp;
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n - i - 1; ++j)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
void PrintArray(int *a, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
}
void CreateArray(int *a, int n)
{
srand(time(NULL));
for (int i = 0; i < n; ++i)
{
a[i] = rand()%100 + 1;
}
}
0 0
- 三种最简单基础的排序 选择排序 冒泡排序 直接插入排序 运用了函数指针数组
- 简单排序:冒泡排序、直接插入排序、直接选择排序
- 三种最简单的排序算法(直接插入排序、冒泡排序、简单选择排序)
- Java基础篇之----排序(快速排序、冒泡排序、堆排序、简单选择排序、 希尔排序、直接插入排序)
- 直接插入排序,选择排序,冒泡排序
- 选择排序、冒泡排序、直接插入排序
- 排序学习(直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序)
- 简单排序---冒泡,简单选择,直接插入
- java数组的排序,选择排序,冒泡排序,插入排序
- 数组的排序(选择/冒泡/插入排序)
- 排序算法---基础算法(冒泡排序,快速排序,选择排序,直接插入排序,桶排序)
- 直接插入、简单选择、冒泡排序
- 简单排序(冒泡,选择,直接插入)
- 直接插入排序、二分插入排序、希尔排序、冒泡排序与简单选择排序
- 数组(冒泡排序,选择排序,插入排序)
- 数组 冒泡排序 选择排序 插入排序
- 数组的简单排序(冒泡排序,简单选择排序,插入排序)
- java实现各种基础排序(冒泡排序、快速排序、直接选择排序、堆排序、直接插入排序、归并排序)
- IOS7中顶部状态栏的设置
- C#遍历数字证书
- js压缩原理
- Android研究院之应用开发线程池的经典使用(二十九)
- VC嵌套VC导致显示位置偏移的解决方法
- 三种最简单基础的排序 选择排序 冒泡排序 直接插入排序 运用了函数指针数组
- 移植开源QT软件-SameGame
- LeetCode Sort List
- Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP
- 英语简单句、并列句、复合句
- 简明vim练级攻略
- android shape的使用
- CentOS yum 源的配置与使用
- 芯客-Rayeager PX2应用之USB摄像头---UVC摄像头篇