将数组中负数调到正数之前,但相对位置不变
2016-05-17 22:14
344 查看
*利用归并排序的思想,时间复杂度为O(logn)
//将数组{1,2,-3,-4,5,6,-1,10}负数调到正数前面,但负数两两之间和正数两两之间顺序不变
//将数组{1,2,-3,-4,5,6,-1,10}负数调到正数前面,但负数两两之间和正数两两之间顺序不变
public class Sort{ public static void main(String[]args){ int []A={1,2,-3,-4,5,6,-1,10}; sort(A,0,A.length-1); for(int i=0;i<A.length;i++){ System.out.print(A[i]+" "); } } public static void sort(int []A,int low,int high){ if(low<high){ int mid=(low+high)/2; sort(A,low,mid); sort(A,mid+1,high); merge(A,low,mid,high); } } public static void merge(int []A,int low,int mid,int high){ int []temp=new int[high-low+1]; int h1=low,h2=mid+1,i=0; while(h1<=mid&&A[h1]<0){ temp[i++]=A[h1++]; } while(h2<=high&&A[h2]<0){ temp[i++]=A[h2++]; } while(h1<=mid){ temp[i++]=A[h1++]; } while(h2<=high){ temp[i++]=A[h2++]; } i=0; while(low<=high){ A[low++]=temp[i++]; } } }
相关文章推荐
- 【原创】.Net 微信 JS-SDK图片、语音上传接口的实现(MVC)-(二 、上传图片服务器操作)
- AWS CLI使用s3
- Gulp开发教程
- 通过脚本生成Zabbix Screen报错Incorrect value for field "vsize"
- Spark中组件Mllib的学习11之使用ALS对movieLens中一百万条(1M)数据集进行训练,并对输入的新用户数据进行电影推荐
- 树莓派spi液晶屏支持(fbtft)nt35510
- 关于error LNK2019::Unresolved External Symbol * Referenced In Function..解决办法 2
- 【数字图像处理】图像风格迁移
- 第8周 C语言及程序设计提高例程-31 编制自己的字符串函数
- JDBC的介绍
- WPF多源绑定
- Tomcat的使用
- 485时序图
- 【Android 第三方SDK】第三方推送服务--个推
- tomcat启用GZIP压缩
- ubuntu 关闭防火墙
- 编译windows版nginx-rtmp-module
- spring统一管理异常和访问日志
- 身份证效验
- mac 第一次安装mysql 5.7.12 不知道root 密码的解决办法