how to use sorting in java without using the collections.sort(list);?

时间:2015-07-28 22:53:11

标签: java sorting

i have this code which i wrote and i wanna know the sorting algorithm to use instead of the Collections.sort(list); function

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

    public class sorting {
        public static void main (String[]args){
            Random I  = new Random();
            List<Integer> list = new ArrayList<Integer>();
            int number; 

            for(int counter=1; counter<=20;counter++){
                number = I.nextInt(100);
                while(list.contains(number)) {
                    number = I.nextInt(100);
                }
                list.add(number);
            }
            Collections.sort(list);
            System.out.println(list);
        }
    }

1 个答案:

答案 0 :(得分:0)

You can implement using Quick Sort (based on Divide and Conquer Algorithm) whose average case is Θ(n log(n)) and in the worst case it is Θ(n2). It sorts by partitioning your array into left and right sub-arrays, then it takes a pivot value and processes the entire array. This is one of the reason why quick sort is also fast. You can find its implementation on Quick Sort. They have done the entire implementation of Quick Sort without the use of Collections.sort(which is basically your requirement too).

Hope this helps!