概述
使用C语言,实现字符串排序算法(冒泡排序)
编码环境:Visual Studio 2022
1、code
char str0[] = "C";
char str1[] = "JAVA";
char str2[] = "C#";
char str3[] = "PHP";
char str4[] = "HTML";
char str5[] = "Python";
char str6[] = "C++";
char str7[] = "JavaScript";
char str8[] = "Go";
char str9[] = "Kotlin";
char* stringArray[] = {str0, str1, str2, str3, str4, str5, str6, str7, str8, str9};
/* 字符串-指针-排序函数 */
void stsrt(char* strings[], int num)
{
char* temp;
int top, seek;
for (top = 0; top < num - 1; top++)
{
for (seek = top + 1; seek < num; seek++)
{
if (strcmp(strings[top], strings[seek]) > 0)
{
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}
}
}
int main(void)
{
int j, strLen;
strLen = sizeof(stringArray) / sizeof(char*);
cout << "Before Sorting\n" << endl;
for (j = 0; j < strLen; j++) {
cout << "\t" << stringArray[j] << endl;
}
stsrt(stringArray, strLen);
cout << "After Sorting\n" << endl;
for (j = 0; j < strLen; j++) {
cout << "\t" << stringArray[j] << endl;
}
return 0;
}
2、运行结果: