Summary: sorting Algorithms

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksortheapsort, or merge sort.

Time Complexity: O(N^2)

for i ← 1 to length(A) - 1

    j ← i

    while j > 0 and A[j-1] > A[j]

        swap A[j] and A[j-1]

        j ← j - 1

 

Bubble sort has worst-case and average complexity both О(n2)

First Pass:
5 1 4 2 8 )  ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 )  ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 )  ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 )  ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
1 4 2 5 8 )  ( 1 4 2 5 8 )
( 1 4 2 5 8 )  ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )

 1 procedure bubbleSort( A : list of sortable items )

 2    n = length(A)

 3    repeat 

 4      swapped = false

 5      for i = 1 to n-1 inclusive do

 6        /* if this pair is out of order */

 7        if A[i-1] > A[i] then

 8          /* swap them and remember something changed */

 9          swap( A[i-1], A[i] )

10          swapped = true

11        end if

12      end for

13    until not swapped

14 end procedure

 

 selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists

Time Complexty: O(N^2)

 1 public class MySelectionSort {

 2  

 3     public static int[] doSelectionSort(int[] arr){

 4          

 5         for (int i = 0; i < arr.length - 1; i++)

 6         {

 7             int index = i;

 8             for (int j = i + 1; j < arr.length; j++)

 9                 if (arr[j] < arr[index])

10                     index = j;

11       

12             int smallerNumber = arr[index]; 

13             arr[index] = arr[i];

14             arr[i] = smallerNumber;

15         }

16         return arr;

17     }

18      

19     public static void main(String a[]){

20          

21         int[] arr1 = {10,34,2,56,7,67,88,42};

22         int[] arr2 = doSelectionSort(arr1);

23         for(int i:arr2){

24             System.out.print(i);

25             System.out.print(", ");

26         }

27     }

28 }

 or

 1 /* a[0] to a[n-1] is the array to sort */

 2 int i,j;

 3 int iMin;

 4  

 5 /* advance the position through the entire array */

 6 /*   (could do j < n-1 because single element is also min element) */

 7 for (j = 0; j < n-1; j++) {

 8     /* find the min element in the unsorted a[j .. n-1] */

 9  

10     /* assume the min is the first element */

11     iMin = j;

12     /* test against elements after j to find the smallest */

13     for ( i = j+1; i < n; i++) {

14         /* if this element is less, then it is the new minimum */  

15         if (a[i] < a[iMin]) {

16             /* found new minimum; remember its index */

17             iMin = i;

18         }

19     }

20  

21     if(iMin != j) {

22         swap(a[j], a[iMin]);

23     }

24  

25 }

 

 Descending Order:

 1 public class descending {

 2          

 3         public static int[] doSelectionSort(int[] arr){

 4              

 5             for (int i = arr.length-1; i >= 1; i--)

 6             {

 7                 int index = i;

 8                 for (int j = 0; j < i; j++)

 9                     if (arr[j] < arr[index])

10                         index = j;

11           

12                 int smallerNumber = arr[index]; 

13                 arr[index] = arr[i];

14                 arr[i] = smallerNumber;

15             }

16             return arr;

17         }

18          

19         public static void main(String[] args){

20              

21             int[] arr1 = {10,34,2,56,7,67,88,42};

22             int[] arr2 = doSelectionSort(arr1);

23             for(int i:arr2){

24                 System.out.print(i);

25                 System.out.print(", ");

26             }

27         }

28 }

 

你可能感兴趣的:(algorithms)