第一次学习程序开发,是程序员中的小白的小白。今天学习完了C语言,为了锻炼分文件编写和函数的使用,所以自己编写了几个自编C语言库函数,主要是对任意数据类型的排序,希望和众多小白一起交流学习。
我是在VS2017中编写的,如图:
很简单,图中有三个文件,一个主文件,一个头文件,一个实现文件,里面又分成了选择排序法和冒泡排序法,如图:
只要调用这两个函数中的任何一个,都可以对字符型或数值型数据进行排序
main.c代码如下:
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"sort.h"
int main()
{
int a[] = { 1,3,7,5,9,4,2,6,8 };
int intLen = sizeof(a) / sizeof(a[0]);
char ch[] = { 'a','e','c','b','t','y','u','w' };
int charLen1 = sizeof(ch) / sizeof(char);
//char ch[] = "helloworld";
//int charLen=strlen(ch);
char* p[] = { "abdc","etkd","keo","lade","hdkve" };
int charLen = sizeof(p) / sizeof(p[0]);
//printf("%d", charLen);
printf("排序前:");
for (int i = 0; i < charLen; i++)
{
printf("%s ", p[i]);
}
putchar('\n');
BobbleSort(3, p, charLen);
printf("排序后:");
for (int i = 0; i < charLen; i++)
{
printf("%s ", p[i]);
}
system("pause");
return 0;
}
sort.c代码如下:
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include "sort.h"
/****************************************************
*
* 选择排序法
*
*****************************************************/
void SelectSort_int(int* p,int len)
{
for (int i = 0; i < len; i++)
{
int s = i;
for (int j = i+1; j < len; j++)
{
if (p[s] > p[j])
{
s = j;
}
}
if (p[i] > p[s])
{
int temp = p[i];
p[i] = p[s];
p[s] = temp;
}
}
return;
}
void SelectSort_char(char* p, int len)
{
for (int i = 0; i < len; i++)
{
int s = i;
for (int j = i + 1; j < len; j++)
{
if (p[s] > p[j])
{
s = j;
}
}
if (p[i] > p[s])
{
int temp = p[i];
p[i] = p[s];
p[s] = temp;
}
}
return;
}
void SelectSort_char2(char* p, int len)
{
char** p1 = (char**)p;
for (int i = 0; i < len; i++)
{
int s = i;
for (int j = i + 1; j < len; j++)
{
if (*p1[s] > *p1[j])
{
s = j;
}
}
if (*p1[i] > *p1[s])
{
char* temp = p1[i];
p1[i] = p1[s];
p1[s] = temp;
}
}
return;
}
void SelectSort(int type, void* p, int len)
{
if (p == NULL || len <= 0)
{
printf("参数2或3出错!");
return;
}
/*type为整型数据,表示要排序的数据类型*/
switch (type)
{
case 1:
SelectSort_int(p, len);
break;
case 2:
SelectSort_char(p, len);
break;
case 3:
SelectSort_char2(p, len);
break;
default:
printf("参数1选择的类型无效!");
break;
}
return;
}
/************选择排序法END***************************/
/****************************************************
*
* 冒泡排序法
*
*****************************************************/
void BobbleSort_int(int* p, int len)
{
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len-1-i; j++)
{
if (p[j] > p[j+1])
{
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
return;
}
void BobbleSort_char(char* p, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
if (p[j] > p[j + 1])
{
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
return;
}
void BobbleSort_char2(char* p, int len)
{
char** p1 = (char**)p;
for (int i = 0; i < len; i++)
{
int s = i;
for (int j = i + 1; j < len; j++)
{
if (*p1[s] > *p1[j])
{
s = j;
}
}
if (*p1[i] > *p1[s])
{
char* temp = p1[i];
p1[i] = p1[s];
p1[s] = temp;
}
}
return;
}
void BobbleSort(int type, void * p, int len)
{
if (p == NULL || len <= 0)
{
printf("参数2或3出错!");
return;
}
/*type为整型数据,表示要排序的数据类型*/
switch (type)
{
case 1:
BobbleSort_int(p, len);
break;
case 2:
BobbleSort_char(p, len);
break;
case 3:
BobbleSort_char2(p, len);
break;
default:
printf("参数1选择的类型无效!");
break;
}
return;
}
/**********************END***************************/