java - 如何在不使用java中任何现有排序方法的情况下将arraylist排序为递增顺序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21519634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort an arraylist into increasing order without using any existing sort methods in java?
提问by user3264287
I need to sort an array of incoming integer values into increasing order, but I can't use any existing sort methods (so no Collections.sort(array)).
我需要将传入的整数值数组排序为递增顺序,但我不能使用任何现有的排序方法(因此没有 Collections.sort(array))。
public void insert(Integer value) {
for (int i = 0; i < array.size(); i++) {
array.add(value);
}
}
Here is my probably over complicated and failed attempt.
这是我可能过于复杂和失败的尝试。
for (int i = 0; i < array.size(); i++) {
array.add(value);
}
if(array.size() >= 2) {
value2 = array.get(0);
for(int j = 0; j < array.size(); j++) {
if(j != 0) {
if(value2 < array.get(j)) {
array.set(0, array.get(j));
array.set(j, value);
}
}
}
}
Any help would be appreciated!
任何帮助,将不胜感激!
回答by Darshan Patel
As people in the comments have said, there are multiple sorting algorithms.
正如评论中的人所说,有多种排序算法。
Here is one of the simplest algorithms that I used in my college (but this use string array):
这是我在大学中使用的最简单的算法之一(但它使用字符串数组):
You can modify it to use a list.
您可以修改它以使用列表。
String temp = "";
for(int i = 0, len = array.length; i < len; i++)
{
for(int j = i + 1; j < len; j++)
{
if(array[i].compareTo(array[j]) > 0)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
回答by therealrootuser
This is an example of a shellsort, which is not the fastest or best sorting algorithm, but will get the job done on a small list.
这是一个 shellsort 的例子,它不是最快或最好的排序算法,但可以在一个小列表上完成工作。
public void sort(Integer[] values)
{
for (int gap = values.length / 2; gap > 0;
gap = gap == 2 ? 1 : (int) (gap / 2.2) )
{
for (int i = gap; i < values.length; i++)
{
Integer tmp = values[i];
int j = i;
for (; j >= gap && tmp.compareTo(values[j-gap]) < 0; j -= gap)
{
a[j] = a[j - gap];
}
a[j] = tmp;
}
}
}
回答by Michael Yaworski
Here is a selection sort. It's one of the easiest sorting algorithms (slower than insertion, but faster than bubble). It compares each element in the list to the rest of the elements in the list. The list will progressively be sorted from the beginning to the end (starting with i = 0
).
这是一个选择排序。它是最简单的排序算法之一(比插入慢,但比冒泡快)。它将列表中的每个元素与列表中的其余元素进行比较。列表将从头到尾逐步排序(以 开头i = 0
)。
i
is comparing to all of the other elements in the list, j
, and when that value at the i
index is found to be greater than the value at the j
index, the values are swapped and the comparisons continue.
i
正在与列表中的所有其他元素进行比较j
,并且当i
发现索引处的值大于j
索引处的值时,交换这些值并继续比较。
public static ArrayList<Integer> selectionSort(ArrayList<Integer> array) {
for (int i = 0; i < array.size() - 1; i++)
{
for (int j = i + 1; j < array.size(); j++)
{
if (array.get(i) > array.get(j)) {
int temp = array.get(j);
array.set(j, array.get(i));
array.set(i, temp);
}
}
}
return array;
}
I'll test it here:
我会在这里测试:
public static void main(String args[])
{
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(5);
list.add(1);
list.add(7);
list.add(19);
list.add(4);
list.add(1);
list = selectionSort(list);
for (int i : list) {
System.out.println(i);
}
}
Output:
输出:
1
1
2
4
5
7
19