fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SIZE 15
  5. #define NAME_SIZE 50
  6.  
  7. void bubbleSort(char towns[][NAME_SIZE],
  8. int size)
  9. {
  10. for (int pass = 0; pass < size - 1; ++pass)
  11. {
  12. int swapped = 0;
  13.  
  14. for (int i = 0; i < size - 1 - pass; ++i)
  15. {
  16. if (strcmp(towns[i], towns[i + 1]) > 0)
  17. {
  18. char hold[NAME_SIZE];
  19.  
  20. strcpy(hold, towns[i]);
  21. strcpy(towns[i], towns[i + 1]);
  22. strcpy(towns[i + 1], hold);
  23.  
  24. swapped = 1;
  25. }
  26. }
  27.  
  28. if (swapped == 0)
  29. {
  30. break;
  31. }
  32. }
  33. }
  34.  
  35. int main(void)
  36. {
  37. char towns[SIZE][NAME_SIZE] =
  38. {
  39. "Cairo",
  40. "Alexandria",
  41. "Giza",
  42. "Luxor",
  43. "Aswan",
  44. "Port Said",
  45. "Suez",
  46. "Tanta",
  47. "Mansoura",
  48. "Minya",
  49. "Fayoum",
  50. "Damietta",
  51. "Ismailia",
  52. "Hurghada",
  53. "Sharm El Sheikh"
  54. };
  55.  
  56. bubbleSort(towns, SIZE);
  57.  
  58. printf("Towns in alphabetical order:\n");
  59.  
  60. for (int i = 0; i < SIZE; ++i)
  61. {
  62. printf("%s\n", towns[i]);
  63. }
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 5344KB
stdin
Standard input is empty
stdout
Towns in alphabetical order:
Alexandria
Aswan
Cairo
Damietta
Fayoum
Giza
Hurghada
Ismailia
Luxor
Mansoura
Minya
Port Said
Sharm El Sheikh
Suez
Tanta