C 冒泡排序原理示范

来源:互联网 发布:minitool数据恢复工具 编辑:程序博客网 时间:2024/06/05 20:31
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//  visual studio 2012 
// C 冒泡排序原理示范

#include "stdafx.h"
#include "iostream"
#include "stdio.h"

//static int test_array[] = {1,2,3,4,5,6};
static int test_array[] = {6,5,4,3,2,1};

void cout_data(void)
{
for (int i =0; i < 6; i++){
if (i < 5){
std::cout << test_array[i];
}
else{
std::cout << test_array[i] << std::endl;
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5-i ;j++){
//if (test_array[j] < test_array[j+1]){
if (test_array[j] > test_array[j+1]){
int temp = 0;
temp = test_array[j];
test_array [j] = test_array[j+1];
test_array[j+1] = temp;
}
cout_data();
}
std::cout << "i = " << i << std::endl;
}
while (1);
return 0;
}

0 0