File tree Expand file tree Collapse file tree 3 files changed +83
-1
lines changed Expand file tree Collapse file tree 3 files changed +83
-1
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,9 @@ PROGRAMS = m_based_demo \
6363btree_demo \
6464sort_demo \
6565fib-heap_demo \
66- scc_demo
66+ scc_demo \
67+ sort_demo \
68+ bubble_sort_demo
6769
6870all : $(PROGRAMS )
6971
Original file line number Diff line number Diff line change 1+
2+
3+
4+ #ifndef _bubble_sort_h_
5+ #define _bubble_sort_h_
6+
7+
8+ #include < iostream>
9+ #include < stdio.h>
10+ #include < stdlib.h>
11+ #include < time.h>
12+
13+
14+ using namespace std ;
15+
16+ namespace aglo {
17+
18+ template <typename T>
19+ static void BubbleSort (T list[],int start,int end){
20+
21+
22+ T tmp;
23+ if (start < end){
24+ for (size_t i = start; i <= end; ++i)
25+ {
26+ for (size_t j = i+1 ; j <= end; ++j)
27+ {
28+ if (list[i]>=list[j]){
29+
30+ tmp=list[i];
31+ list[i]=list[j];
32+ list[j]=tmp;
33+
34+ }
35+
36+ }
37+
38+ }
39+
40+ }
41+
42+ }
43+
44+
45+
46+
47+
48+ }
49+
50+
51+ #endif
Original file line number Diff line number Diff line change 1+ #include < stdio.h>
2+ #include < stdlib.h>
3+ #include < time.h>
4+
5+ #include " generic.h"
6+ #include " bubble_sort.h"
7+
8+
9+
10+ using namespace alg ;
11+ int main (int argc, char const *argv[])
12+ {
13+ const int MAX_ELEMENTS = 10 ;
14+ int list[MAX_ELEMENTS];
15+ for (int i = 0 ; i < MAX_ELEMENTS; i++ ){
16+ list[i] = rand ()%100 ;
17+ }
18+ printf (" The list before sorting is:\n " );
19+ printlist (list,MAX_ELEMENTS);
20+
21+ // sort the list using quicksort
22+ aglo::BubbleSort (list,0 ,MAX_ELEMENTS-1 );
23+
24+ // print the result
25+ printf (" The list after sorting using quicksort algorithm:\n " );
26+ printlist (list,MAX_ELEMENTS);
27+
28+ return 0 ;
29+ }
You can’t perform that action at this time.
0 commit comments