the improved BubbleSort.
2015-01-15 09:58
567 查看
the bubble sort is easy to use,but the effiency is pretty low.How we solve the problem?
when we use bubble sort,we know when we sort the data again and again,it seems that there is a status that the sort has already sorted,but we also scan the data,so,it is an abuse.In order to make it more efficient,we can use
a flag to judge the scanner's status,if the scanner is still working.we continue the process of scan.While the scanner is stop,we can make the flag into 1 status,so we can jump out through the circulation.the method is very famous:more space instead of time.the
following is the realization.
void bubblesort(int a[],int n)
{
int i,j,flag=0,r;
for(i=0;i<n-1;i++)
{
for(j=n-1;j>i;j--)
{
if(a[j]<a[j-1])
{
r=a[j-1];
a[j-1]=a[j];
a[j]=r;
flag = 1;//there has the exchange of data
}
}
if(flag = 0)
break;
else
flag = 0;
}
}
when we use bubble sort,we know when we sort the data again and again,it seems that there is a status that the sort has already sorted,but we also scan the data,so,it is an abuse.In order to make it more efficient,we can use
a flag to judge the scanner's status,if the scanner is still working.we continue the process of scan.While the scanner is stop,we can make the flag into 1 status,so we can jump out through the circulation.the method is very famous:more space instead of time.the
following is the realization.
void bubblesort(int a[],int n)
{
int i,j,flag=0,r;
for(i=0;i<n-1;i++)
{
for(j=n-1;j>i;j--)
{
if(a[j]<a[j-1])
{
r=a[j-1];
a[j-1]=a[j];
a[j]=r;
flag = 1;//there has the exchange of data
}
}
if(flag = 0)
break;
else
flag = 0;
}
}
相关文章推荐
- Java改进的冒泡排序ImprovedBubbleSort
- The bubble sort and Binary search
- 2014.2.25 改进型冒泡排序 improved bubble sort algorithm
- Use bubble sort to sort the input intergers
- 【寒江雪】B.Sort the array
- Impact of the sort buffer size in MySQL
- Aizu ALDS1_2_A Bubble Sort(冒泡排序)
- The Smallest String Concatenation CodeForces - 632C (string sort)
- 《挑战程序设计竞赛》阅读笔记二 之 ALDS1_2_A Bubble Sort
- 冒泡排序(Bubble_Sort)
- Recursive version of the insertion sort
- AOJ-ALDS1_2_A Bubble Sort【冒泡排序】
- 数据结构基础(1) --Swap & Bubble-Sort & Select-Sort
- The Sort Test Code of Supersonic
- Bubble_Sort
- Ignatius and the Princess IV---hdu1029(动态规划或者sort)
- Program work 18. Bubble Sort in Java
- an improved counting sort for handling negative integers, also take an easy performance compare
- Poj 1974 The Happy Worm(sort结构体二级排序)
- Mathmatical Prove of the Effectiveness of Quick Sort