C Language: Use bubble sort to sort multiple strings (optimizations).

Source: Internet
Author: User

#define  _CRT_SECURE_NO_WARNINGS 1#include<stdio.h> #include <stdlib.h> #include <string.h >int main () { char *str[] = {  "Hello",  "change",  "World",  "come ", " on " };   //pointer array, each of which is a character pointer  int  i = 0; int   j = 0; int flag; int size = sizeof (str)  / sizeof (str[0 ]); for  (i = 0; i < size - 1; i++)    // n Strings, swap (n-1) times  {      flag = 1; //set flag bit, optimize bubble        for  (j = 0; j <size - 1 - i; j++)       {           if   (strcmp (str[j], str[j + 1])  > 0)   //constant string at the address of the space             {                 char *tmp = null;  //Exchange Address                  tmp = str[j];                 str[j] = str[j + 1];  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STR[J&NBSP;+&NBSP;1]  = tmp;                 flag = 0;       }  }       if  (flag)    //Description does not enter the if internal, that is, the required output order has not been swapped       {            break;      }  } for  (i = 0; i < size; i++)  {      printf ("%s ",  str[i]);  } printf ("\ n");  system ("pause");  return 0;}

650) this.width=650; "title=" string interchange. png "src=" http://s3.51cto.com/wyfs02/M00/75/F3/ Wkiol1zfnejxgrq2aabl-oo3hom938.png "alt=" Wkiol1zfnejxgrq2aabl-oo3hom938.png "/>

C Language: Use bubble sort to sort multiple strings (optimizations).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.