`
skychongrichie
  • 浏览: 3361 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

一种时间复杂度为O(n)的排序方式

阅读更多

代码如下:

 

import java.util.BitSet;

import com.sun.java_cup.internal.internal_error;

 

public class Sort

{

private static boolean[] temp=new boolean[10000];

/**

* @notice 注意参数中的array数组中的每个元素大小不能超过9999,而且不能有重复元素。

* 同时也就意味着array数组大小不能超过10000,其中元素大小在0-9999的这样一个范围。

*/

public void sort(int[] array)

{

init();

for(int i=0;i<array.length;i++)

{

temp[array[i]]=true;

}

int loc=0;

for(int j=0;j<temp.length;j++)

{

if(temp[j])

array[loc++]=j;

}

}

private void init()//其实在此方法中通过传一个整数可以剪枝,而整数为array数组中元素的最大值

{

for(int i=0;i<temp.length;i++)

temp[i]=false;

}

public void print(int[] array)

{

for(int i=0;i<array.length;i++)

System.out.print(array[i]+"  ");

System.out.println();

}

public static void main(String[] args)

{

Sort sort=new Sort();

int array[]=new int[]{3,5,1,2,10,88,25,66};

sort.print(array);

sort.sort(array);

sort.print(array);

}

}

*****************运行结果*****************

 

3  5  1  2  10  88  25  66  

1  2  3  5  10  25  66  88  

 

今天在看《编程珠玑》开始的时候,作者提到了位示图的数据结构,受其启发想到了这样一种排序方法,不过这种排序算法对传入的数组有着严格要求,不能重复,而且数组中元素的数值范围受辅助数组的大小的制约,但是此排序算法相对来说还是很快的,时间复杂度为O(n)。感觉美中不足的是貌似主流编程语言中没有提供一种bit的数据类型,不然会大大减小前边提到的元素数值范围所受的限制。本人还是一位刚入职的菜鸟,鉴于所学有限,不足之处望大家能多多指正。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics