C 字符串数组排序的小例子_C 语言

复制代码 代码如下:

#include<stdio.h>
#include<string.h>
#include <malloc.h>
void q_sortB(char str[20][20], int n);
void qs(char str[20][20],int n);

void main() {
    int i, n;
    char str[20][20] = { { "Adam" }, { "Bob" }, { "Dimen" }, { "Colin" }, {
            "Correal" }, { "Sick" }, { "Rachel" } };

    char * str1[20]= { { "Adam" }, { "Bob" }, { "Dimen" }, { "Colin" },
            { "Correal" }, { "Sick" }, { "Rachel" } };
    qs(str,7);
    q_sortB(str, 7);
    for (i = 0; i < 7; i++){
        printf("%s\n", str[i]);
    }

}

void qs(char str[20][20],int n){
    char temp[20];
    int i=0;
    int j=0;
    int min=i;
    for(i=0;i<n-1;i++){
        min=i;
        for(j=i;j<n;j++){//本次找最小值的范围是从i开始  到最末尾
            if(  strcmp(str[j],str[min])==-1  ){
                min=j;
            }
        }
        //此时min指向最小的
        //那么应该把min放在已排序部分的后一个//也就是本次排序的第一个
        strcpy(temp,str[i]);
        strcpy(str[i],str[min]);
        strcpy(str[min],temp);

    }
}

//泡泡
void q_sortB(char str[20][20], int n) {
    char a[20];
    int i, j;
    for (i = 0; i < n-1; i++) {
        for (j = i ; j < n-1; j++)
            if (strcmp(str[j], str[j + 1]) > 0) {
                strcpy(a, str[j]);
                strcpy(str[j], str[j + 1]);
                strcpy(str[j+1], a);
            }
    }

}

时间: 2024-07-30 14:37:04

C 字符串数组排序的小例子_C 语言的相关文章

C/C++语言中结构体的内存分配小例子_C 语言

当未用 #pragma 指令指定编译器的对齐位数时,结构体按最长宽度的数据成员的宽度对齐:当使用了 #pragma 指令指定编译器的对齐位数时,结构体按最长宽度的数据成员的宽度和 #pragma 指令指定的位数中的较小值对齐. #pragma 指令格式如下所示:#pragma pack(4)     // 或者 #pragma pack(push, 4) 举例如下:(机器字长为 32 位)    struct    {        char a;    }test;    printf("%d

C语言数组指针的小例子_C 语言

1.功能:输入6个学生的5门课程成绩,计算出每个学生的平均分和每门课程的平均分.2.C语言实现代码:(其实就是用二维数组来实现的,二维数组的引用传递使用数组指针来完成) 复制代码 代码如下: #include <stdio.h>#define STUDENT 5#define SCORE 6void input_array(float (*score)[STUDENT]);void avg_score(float (*score)[STUDENT]);void avg_course(float

C语言获得电脑的IP地址的小例子_C 语言

复制代码 代码如下: #include <stdio.h> #include <winsock2.h>  #pragma comment(lib, "WS2_32.lib")  int main() {     char host_name[256]; // define host name (for example:xxx-PC)     int WSA_return, i;     WSADATA WSAData;     HOSTENT *host_ent

C语言判断回文数的小例子_C 语言

复制代码 代码如下: #include<stdio.h>#include<stdlib.h> int is_palindrome(char* para_str , int len); int main(int argc , char* argv[]){   int n = atol(argv[2]);     if (is_palindrome(argv[1],n))       printf("this string is palindrome !\n"); 

异步http listener 完全并发处理惩罚http恳求的小例子_C 语言

复制代码 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using DevSDK.Net.Sockets; using System.IO;  namespace ConsoleApplication1 {     class Program     {         s

short与int转换的小例子_C 语言

复制代码 代码如下: #include <stdio.h> int main(){    short a=-1;    unsigned int b=a;    int c=a;     printf("%x\n", b);    printf("%d\n", c);     a=1;    b=a;    c=a;     printf("%x\n", b);    printf("%d\n", c);    

c语言读取obj文件转换数据的小例子_C 语言

复制代码 代码如下: // hello.cpp : Defines the entry point for the console application.// #include "stdafx.h"#include "stdio.h" int _tmain(int argc, _TCHAR* argv[]){    FILE *file1,*file2;    file1=fopen("047facesmall.obj","r&quo

c语言在控制台判定鼠标左键的小例子_C 语言

复制代码 代码如下: // temp1.cpp : Defines the entry point for the console application. //  //#include <stdafx.h> #include <windows.h> #include <conio.h> #include <stdlib.h> #include<stdio.h> int main(int argc, char* argv[]) {  SetCon

解析C++中的字符串处理函数和指针_C 语言

C++字符串处理函数 字符串连接函数 strcat 其函数原型为 strcat(char[],const char[]); strcat是string catenate(字符串连接)的缩写.该函数有两个字符数组的参数,函数的作用是:将第二个字符数组中的字符串连接到前面字符数组的字符串的后面.第二个字符数组被指定为const,以保证该数组中的内容不会在函数调用期间修改.连接后的字符串放在第一个字符数组中,函数调用后得到的函数值,就是第一个字符数组的地址.例如: char str1[30]=″Peo