一、冒泡排序
冒泡排序有向上冒泡和向下冒泡,这里以向下冒泡为例说明。
向下冒泡的思路:比较相邻的两个数据,如果前一个数据比后一个数据大,那么就交换数据。依次这样进行。下面我们使用了两种方法实现。
一种是直接按冒泡的思路直接写的,而后一种设置了一个标记,其实相当于对冒泡排序进行了小小的优化。
二、代码实现
// 冒泡排序.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void BubbleSort1(int a[],int n);
void BubbleSort2(int a[],int n);
void Swap(int *x1, int *x2);
int _tmain(int argc, _TCHAR* argv[])
{
int Array[4]={1,-1,3,4};
for ( int i=0; i< 4; i++)
cout << Array[i] << " ";