Basic Sort Algorithms

Source: Internet
Author: User

1. Bubble Sort
public void Bubblesort (int[] arr) {  Boolean swapped = true;  int j = 0;  int tmp;  while (swapped) {    swapped = false;    j + +;    for (int i = 0; i < arr.length-j; i++) {      if (Arr[i] > arr[i + 1]) {      tmp = arr[i];      Arr[i] = arr[i + 1];      Arr[i + 1] = tmp;      swapped = true;      }    } }}

Performance

Worst case Performance O (n^2)
Best Case Performance O (n)
Average Case Performance O (n^2)
Worst case space Complexity O (1) Auxiliary

2. Selection Sort
  public void Doselectionsort (int[] arr) {for     (int i = 0; i < arr.length-1; i++) {      int index = i;      for (int j = i + 1; j < Arr.length; J + +) {         if (Arr[j] < Arr[index]) {            index = j;         }      }      int smallernumber = Arr[index];       Arr[index] = Arr[i];      Arr[i] = Smallernumber;   }  }

Performance

Worst case performanceо (N2)
Best Case performanceо (n2)
Average case Performanceо (n2)
Worst case Space Complexityо (n) Total, O (1) Auxiliary

3. Insertion Sort
public static void Insertionsort (int array[]) {  int n = array.length;  for (int j = 1; j < N; j + +) {    int key = Array[j];    int i = j-1;    while ((I >-1) && (array [i] > key) {array [      i+1] = array [i];      i--;    }    ARRAY[I+1] = key;  }}

Performance

Worst case performanceо (n2) comparisons, swaps
Best Case performance O (n) comparisons, O (1) Swaps
Average case Performanceо (n2) comparisons, swaps
Worst case Space Complexityо (n) Total, O (1) Auxiliary

Comparison:

There ' s probably no point in using the bubble sort, unless you don't have your algorithm book handy. The bubble sort is so simple, you can write it from memory. Even so, it's practical only if the amount of data is small.

The selection sort minimizes the number of swaps, but the number of comparisons was still high. This sort might was useful when the amount of data is small and swapping data items are very time-consuming compared with co Mparing them. The insertion sort is the most versatile of the three and are the best bet on the most situa-tions, assuming the amount of DAT A is small or the data is almost sorted. For larger amounts of data, quicksort is generally considered the fastest approach.

It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

We ' ve compared the sorting algorithms in terms for speed. Another consideration for any algorithm was how much memory space it needs. All three of the algorithms in this chapter carry out their sort on place, meaning that, besides the initial array, very l Ittle extra memory is required. All the sorts require a extra variable to store a item temporarily while it ' s being swapped.

Basic Sort Algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.