This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

flow chart of C language for bubble sort

D:\CPPcode\bubblesort\bubblesort.c++
 1 //++++++++++++++++++++++++++
 2  //      bubblesort.cpp
 3  //++++++++++++++++++++++++++
 4  
 5  ///termino
 6  ///#include head file
 7  #include <stdio.h> // C
 8  //#include <iostream> // C++
 9  
10  
11  ///main_entry
12  int main()
13  {
14      
15      
16      ///declare
17      int i, j, n, temp, bubble[10];
18      
19      
20      ///  Prompt sort N
21      printf("Enter 2~10 to be sorted:");
22      scanf("%d",&n);
23      
24      
25      ///Loop N
26      for(i=0; i<n; i++)
27      {
28          
29          
30          ///  Prompt & keyin
31          printf("Enter bubble[%d]:",i);
32          scanf("%d",&bubble[i]);
33          
34          
35          ///+
36      }//end for
37      
38      
39      ///Prompt pre-sort
40      printf("\n Pre-sort data:");
41      
42      
43      /// loop show pre-sort data
44       for(i=0; i<n; i++) printf("%d, ", bubble[i]);
45      
46      
47      ///Loop N
48      for(i=0; i<n; i++)
49      {
50          
51          
52          ///Loop N-1
53          for(j=0; j<n-i-1; j++)
54          {
55              
56              
57              ///left> right
58              if(bubble[j] > bubble[j+1])
59              {
60                  
61                  
62                  ///left-right swap
63                  temp = bubble[j];
64                  bubble[j] = bubble[j+1];
65                  bubble[j+1] = temp;  
66                  
67                  
68                  
69                  /// +
70              }
71              
72              
73              ///+
74          }
75          
76          
77          ///+
78      }
79      
80      
81      ///Prompt post-sort
82      printf("\npost-sort data:");
83      
84      
85      ///loop show post-sort data
86      for(i=0; i<n; i++) printf("%d, ", bubble[i]);
87      
88      
89      ///  main_end
90      return 0;
91  } //main_end
92  
93  

@ 下載程式碼 Download source code

沒有留言:

張貼留言