快速排序在不同的IDE上运行结果的差异
最近写了一个快速排序,一开始是在codeblocks编写的,编译和运行都正常,截图如如下:
为了测试不同的IDE下开发的差异,所以我又在vs2010中测试了一下,结果却让人大跌眼镜啊,编译正常,但是在运行出现了错误,截图如下:
为什么相同的代码,却得到不同的结果呢?郁闷!
之后我又在Linux上进行了测试,得到的结果和在vs2010上得到的结果很相似,没有成功!
所以到论坛里来请教一下各位,麻烦帮忙看看是怎么回事?或者给点建议.不胜感激啊!
下面将代码贴出来:
codeblock vs2010 linux
/**
* quickSort.c
* Date: 2013-03-26
* Author: zff
* Description: simple quick sortion
*/
#include<stdio.h>
/*函数声明*/
void quickSort(int arr[], int low, int high);
int findPosition(int arr[], int low, int high);
/*主函数*/
int
main(void)
{
int array[] = {5, 2, 6, 8, 4, 3, 7};
int i ;
quickSort(array, 0, 7);
/*打印排序结果*/
printf("快速排序之后的顺序: \n");
for(i = 0; i < 7; i++){
printf("%2d", array[i]);
}
return 0;
}
/**
* 函数名: quickSort()
* 函数参数: arr[]对数组中的元素进行排序;
* low 第一个元素的下标
* high 最后一个元素的下标
* 函数返回值:void
* 函数功能: 对一维数组中的元素进行快速排序
*/
void
quickSort(int arr[], int low, int high)
{
int position;
if(low < high){
position = findPosition(arr, low, high);
//递归
quickSort(arr, low, position-1);
quickSort(arr, position+1, high);
}
}
/**
* 函数名: findPosition()
* 函数参数: 与函数quickSort()中的参数意义相同
* 函数返回值: 返回元素的位置
* 函数功能: 找到元素的位置
*/
int
findPosition(int arr[], int low, int high)
{
int value = arr[low];//value是主元,即参考元素.将第一个元素设置为主元
while(low < high){
//从后开始比较
while(low < high && arr[high] >= value)
--high;
arr[low] = arr[high];
//从前开始比较
while(low < high && arr[low] <= value)
++low;
arr[high] = arr[low];
}
arr[low] = value;
return high;
}