Skip to content

Commit 60b9ec3

Browse files
committed
Merge branch 'master' of https://github.com/wyh267/algorithms into wyh267-master
Conflicts: Makefile
2 parents 91493ec + 8a80a72 commit 60b9ec3

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ PROGRAMS = m_based_demo \
6363
btree_demo \
6464
sort_demo \
6565
fib-heap_demo \
66-
scc_demo
66+
scc_demo \
67+
sort_demo \
68+
bubble_sort_demo
6769

6870
all: $(PROGRAMS)
6971

include/bubble_sort.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 voidBubbleSort(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

src/bubble_sort_demo.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)