想搞个快速排序,却用上了ArrayList是不是有点“邪魔外道” ……
2015-09-14 00:10
375 查看
想搞个快速排序,却用上了ArrayList是不是有点“邪魔外道” ……
import java.util.ArrayList;
/**
* Created by Monster on 2015/9/13.
* 使用可变数组进行类似于快速排序的排序:
* 方法checkSequen用法:
* 接收一个已排序的可变数组,及一个待比较的值;
* 即可输出这个值在这个已排序的数值中应该插入的位置,即下标;
*
* 方法quickSortByArrayList用法:
* 接收一个未排序的数组,然后新建一个为空的outputArrayList;
* 并对为排序的数组中的每一个元素及outputArrayList使用checkSequen;
* 并按大小插入到outputArrayList中;
* 最后将可变数组转换成数组outputArray输出;
*/
public class QuickSortByArrayList {
int index;
int indexInCheck;
}
import java.util.ArrayList;
/**
* Created by Monster on 2015/9/13.
* 使用可变数组进行类似于快速排序的排序:
* 方法checkSequen用法:
* 接收一个已排序的可变数组,及一个待比较的值;
* 即可输出这个值在这个已排序的数值中应该插入的位置,即下标;
*
* 方法quickSortByArrayList用法:
* 接收一个未排序的数组,然后新建一个为空的outputArrayList;
* 并对为排序的数组中的每一个元素及outputArrayList使用checkSequen;
* 并按大小插入到outputArrayList中;
* 最后将可变数组转换成数组outputArray输出;
*/
public class QuickSortByArrayList {
int index;
int indexInCheck;
public int checkSequence(ArrayList<Integer> ArrayList,int input){ for(int i=0;i<ArrayList.size();i++){ if (input<=ArrayList.get(i)){ indexInCheck=i; break; } else { indexInCheck=-1; } } return indexInCheck; } public int[] quickSortByArrayList(int[] inputArray){ ArrayList<Integer> outputArrayList=new ArrayList<Integer>(0); for(int j=0;j<inputArray.length;j++){ index=checkSequence(outputArrayList,inputArray[j]); if(index==-1){ outputArrayList.add(inputArray[j]); } else { outputArrayList.add(index,inputArray[j]); } } int[] outputArray=new int[outputArrayList.size()]; for(int i=0;i<outputArrayList.size();i++){ outputArray[i]=outputArrayList.get(i); } return outputArray; }
}
相关文章推荐
- 快速排序
- 文件遍历排序函数
- ruby 数组使用教程
- Ruby中的数组和散列表的使用详解
- C#实现AddRange为数组添加多个元素的方法
- C#选择排序法实例分析
- C#动态调整数组大小的方法
- C#插入法排序算法实例分析
- 详解Lua中的数组概念知识
- C#实现Datatable排序的方法
- Perl中的列表和数组学习笔记
- 探索PowerShell (八) 数组、哈希表(附:复制粘贴技巧)
- C#中数组初始化与数组元素复制的方法
- C#交错数组用法实例
- SQLSERVER的排序问题结果不是想要的
- PowerShell数组的一些操作技巧
- Windows Powershell排序和分组管道结果
- C#通过yield实现数组全排列的方法
- C#不重复输出一个数组中所有元素的方法
- C#实现将数组内元素打乱顺序的方法