本文整理汇总了C++中bubble_sort函数的典型用法代码示例。如果您正苦于以下问题:C++ bubble_sort函数的具体用法?C++ bubble_sort怎么用?C++ bubble_sort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了bubble_sort函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: main
int main() {
std::vector<int> V = {1, 1, 0, 3, 4, 5, 1, 0, 5, 5, 3, 1, 2, 2, 2, 2};
bubble_sort(V);
printv(V);
return 0;
}
开发者ID:iamslash,项目名称:learn_to_code,代码行数:9,代码来源:a.cpp
示例2: test_to_sort_the_elements_list_of_double_datatype_when_list_is_already_sorted
void test_to_sort_the_elements_list_of_double_datatype_when_list_is_already_sorted(){
double before_sorting[3] = {4.0,7.0,8.0};
double after_sorting[3] = {4.0,7.0,8.0,};
void* elements[3] = {&before_sorting[0],&before_sorting[1],&before_sorting[2]};
bubble_sort(elements,3,compare_doubles);
ASSERT(after_sorting[0] == *(double*)elements[0]);
ASSERT(after_sorting[1] == *(double*)elements[1]);
ASSERT(after_sorting[2] == *(double*)elements[2]);
};
开发者ID:Prajaktakudale,项目名称:DSA,代码行数:10,代码来源:bubble_sortTest.c
示例3: test_to_sort_the_elements_list_of_float_datatype_when_element_are_duplicate
void test_to_sort_the_elements_list_of_float_datatype_when_element_are_duplicate(){
float before_sorting[3] = {7.0f,7.0f,4.0f};
float after_sorting[3] = {4.0f,7.0f,7.0f};
void* elements[3] = {&before_sorting[0],&before_sorting[1],&before_sorting[2]};
bubble_sort(elements,3,compare_floats);
ASSERT(after_sorting[0] == *(float*)elements[0]);
ASSERT(after_sorting[1] == *(float*)elements[1]);
ASSERT(after_sorting[2] == *(float*)elements[2]);
};
开发者ID:Prajaktakudale,项目名称:DSA,代码行数:10,代码来源:bubble_sortTest.c
示例4: test_to_sort_the_elements_list_of_int_datatype_when_element_are_duplicate
void test_to_sort_the_elements_list_of_int_datatype_when_element_are_duplicate(){
int before_sorting[3] = {4,2,2};
int after_sorting[3] = {2,2,4};
void* elements[3] = {&before_sorting[0],&before_sorting[1],&before_sorting[2]};
bubble_sort(elements,3,compare_ints);
ASSERT(after_sorting[0] == *(int*)elements[0]);
ASSERT(after_sorting[1] == *(int*)elements[1]);
ASSERT(after_sorting[2] == *(int*)elements[2]);
};
开发者ID:Prajaktakudale,项目名称:DSA,代码行数:10,代码来源:bubble_sortTest.c
示例5: test_to_sort_the_elements_list_of_int_datatype_when_list_is_already_sorted
void test_to_sort_the_elements_list_of_int_datatype_when_list_is_already_sorted(){
int before_sorting[3] = {1,2,3};
int after_sorting[3] = {1,2,3};
void* elements[3] = {&before_sorting[0],&before_sorting[1],&before_sorting[2]};
bubble_sort(elements,4,compare_ints);
ASSERT(after_sorting[0] == *(int*)elements[0]);
ASSERT(after_sorting[1] == *(int*)elements[1]);
ASSERT(after_sorting[2] == *(int*)elements[2]);
};
开发者ID:Prajaktakudale,项目名称:DSA,代码行数:10,代码来源:bubble_sortTest.c
示例6: test_to_sort_the_elements_list_of_string_datatype_when_element_are_duplicate
void test_to_sort_the_elements_list_of_string_datatype_when_element_are_duplicate(){
String before_sorting[3] = {"dddd","bbcc","bbcc"};
String after_sorting[3] = {"bbcc","bbcc","dddd"};
void* elements[3] = {&before_sorting[0],&before_sorting[1],&before_sorting[2]};
bubble_sort(elements,3,compare_chars);
ASSERT(0==strcmp(after_sorting[0],*(String*)elements[0]));
ASSERT(0==strcmp(after_sorting[1],*(String*)elements[1]));
ASSERT(0==strcmp(after_sorting[2],*(String*)elements[2]));
};
开发者ID:Prajaktakudale,项目名称:DSA,代码行数:10,代码来源:bubble_sortTest.c
示例7: test_to_sort_the_elements_list_of_char_datatype_when_element_are_duplicate
void test_to_sort_the_elements_list_of_char_datatype_when_element_are_duplicate(){
char before_sorting[3] = {'d','c','c'};
char after_sorting[3] = {'c','c','d'};
void* elements[3] = {&before_sorting[0],&before_sorting[1],&before_sorting[2]};
bubble_sort(elements,3,compare_chars);
ASSERT(after_sorting[0] == *(char*)elements[0]);
ASSERT(after_sorting[1] == *(char*)elements[1]);
ASSERT(after_sorting[2] == *(char*)elements[2]);
};
开发者ID:Prajaktakudale,项目名称:DSA,代码行数:10,代码来源:bubble_sortTest.c
示例8: main
int main(void)
{
mylib::date dates[] = {mylib::date(1912,6,23), mylib::date(1941,9,9),
mylib::date(1943,2,4), mylib::date(1931,10,12),
mylib::date(1926,8,27)};
size_t ndates = sizeof dates / sizeof dates[0];
bubble_sort(dates, ndates);
std::ostream_iterator<mylib::date> out_it(std::cout,"\n");
std::copy(dates, dates + ndates, out_it);
}
开发者ID:Chaitanyagoyal14,项目名称:two-day-courses,代码行数:10,代码来源:date3_sortdemo.cpp
示例9: main
int main(int argc,char* argv[])
{
const int len=10;
int ary[10]={0,4,6,2,3,1,5,7,9,8};
print(ary,len);
bubble_sort(ary,len);
print(ary,len);
return 0;
}
开发者ID:cacard,项目名称:Algorithm,代码行数:10,代码来源:100.sort.bubble.c
示例10: main
int main(int argc, const char *argv[])
{
int i;
int d[N]= {48,38,65,97,76,13,27,49};
bubble_sort(d, N);
for (i = 0; i < N; i++) {
printf("%d \n",d[i]);
}
return 0;
}
开发者ID:wcybxzj,项目名称:shangguan,代码行数:10,代码来源:bubble.c
示例11: main
int main(){
int arr[]={-8,-3,-4,-6,-77,-88,-101};
int arr_size=6;
bubble_sort( arr, arr_size );
print_array( arr, arr_size );
return 0;
}
开发者ID:lokeshw24,项目名称:5_Sorting_and_Searching,代码行数:10,代码来源:5.3_old_bubble_sort.c
示例12: test_sort
void test_sort(int *numbers, int count, compare_cb cmp) {
int i = 0;
int *sorted = bubble_sort(numbers, count, cmp);
if (!sorted) die("Sorting fail.");
for (i = 0; i < count; i++) {
printf("%d ", sorted[i]);
}
printf("\n");
free(sorted);
}
开发者ID:saisai,项目名称:c-2,代码行数:10,代码来源:ex18.c
示例13: main
int main()
{
int N[10] = {1, 5, 4, 3, 2, 10, 7, 6, 8, 9};
rand_array(N);
print_array(N, 0);
bubble_sort(N, 10);
print_array(N, 0);
}
开发者ID:iamslash,项目名称:dsalgorithm,代码行数:10,代码来源:a.c
示例14: main
int main(int argc,char** argv)
{
int array[9] = {9,7,5,4,6,8,1,3,2};
bubble_sort(array,9);
int i;
for(i=0;i<9;i++){
printf("current:%d \n",array[i]);
}
return 0;
}
开发者ID:onlinedj,项目名称:native_test,代码行数:10,代码来源:s_bubble_sort.c
示例15: main
int main(int argc, const char * argv[])
{
std::vector<int> array = create_random_array();
std::cout << " Before sorting: " << std::endl;
printArray(array);
bubble_sort(array);
std::cout << " After sorting: " << std::endl;
printArray(array);
return 0;
}
开发者ID:wonjoolee95,项目名称:algorithms,代码行数:10,代码来源:bubble_sort.cpp
示例16: main
int main(int argc, char argv[])
{
int arr[] = {3, 5, 2, 7, 0};
int len = sizeof(arr)/sizeof(int);
bubble_sort(arr, len);
print_res(arr, len);
return 0;
}
开发者ID:ManBeMe,项目名称:algorithms,代码行数:10,代码来源:bubble_sort.c
示例17: main
int main() {
int arr[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 };
int len = (int) sizeof(arr) / sizeof(*arr);
bubble_sort(arr, len);
int i;
for (i = 0; i < len; i++){
printf("%d ", arr[i]);
}
return 0;
}
开发者ID:Zhu-Zhi-Hao,项目名称:shamukernelBinary,代码行数:10,代码来源:bubble_sort.c
示例18: main
int main()
{
int num[N] = {89, 38, 11, 78, 96, 44, 19, 25};
bubble_sort(num, N); //或者使用bubble_sort_better(num, N);
for(int i=0; i<N; i++)
printf("%d ", num[i]);
printf("\n");
system("pause");
return 0;
}
开发者ID:chibaofang,项目名称:Data-structure-and-algorithms,代码行数:10,代码来源:merge_sort.c
示例19: main
int main()
{
// 1. get valid filename and open that file
char fname[500];
FILE* f;
do {
printf("Enter filename with absolute path --> ");
gets_s(fname, sizeof(fname));
f = fopen(fname, "r");
if (f == NULL)
printf("Error! I could not open the specified file %s\n", fname);
} while (f == NULL);
// 2. read in all the numbers
char line[500];
int counter = 0;
double A[1000];
while (fgets(line, sizeof(line), f))
{
char* errptr1;
double d = strtod(line, &errptr1);
printf("Number #%d: %.5f\n", counter, d);
// if the only thing from line that cannot be converted to a double
// by strtod() is the newline character \n and the null termination
// character \0, then everything was ok!
if (strcmp(errptr1, "\n") == 0)
{
A[counter] = d;
}
else
{
printf("Error: some characters of the string could not be converted!\n");
_getch();
}
counter++;
}
// 3. sort the array using Bubble sort https://en.wikipedia.org/wiki/Bubble_sort
bubble_sort(A,counter);
// 4. write sorted array to another file
char fname2[1000];
sprintf(fname2, "%s.sorted.txt", fname);
FILE* f2=fopen(fname2, "w");
for (int i=0; i<counter; i++)
fprintf(f2, "%.5f\n", A[i]);
fclose(f2);
printf("Press any key to exit this program.");
_getch();
}
开发者ID:niklasstich,项目名称:Solutions-Exercises-Programming1,代码行数:55,代码来源:I2.c
示例20: main
int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "usage: %s <sort algorithm> <array size>\n", argv[0]);
exit(-1);
}
int count = atoi(argv[2]);
int *array = (int*)malloc(sizeof(int) * count);
if (array == NULL) {
perror("malloc error");
exit(-1);
}
int i;
srand(time(NULL));
printf("sorting");
for (i = 0; i < count; ++i) {
array[i] = rand() & 0xff;
printf(" %d", array[i]);
}
printf("\n");
if (strcmp(argv[1], "bubble") == 0) {
bubble_sort(array, count);
} else if (strcmp(argv[1], "merge") == 0) {
merge_sort(array, 0, count, NULL);
} else if (strcmp(argv[1], "insert") == 0) {
insert_sort(array, count);
} else if (strcmp(argv[1], "heap") == 0) {
heap_sort(array, count);
} else if (strcmp(argv[1], "quick") == 0) {
quick_sort(array, 0, count);
} else {
fprintf(stderr, "unknown sort algorithm\n");
exit(-1);
}
printf("result is");
for (i = 0; i < count - 1; ++i) {
printf(" %d", array[i]);
if (array[i] > array[i+1]) {
printf("sort error\n");
for (i = 0; i < count; ++i) {
printf("%d ", array[i]);
}
exit(-1);
}
}
if (count > 0) {
printf(" %d", array[i]);
}
printf("\nsort OK\n");
return 0;
}
开发者ID:haow1990,项目名称:algorithms,代码行数:55,代码来源:sort_test.c
注:本文中的bubble_sort函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论