C # data structure and algorithm Learning Series 4-Basic Sorting Algorithm

Source: Internet
Author: User

The two most common operations to store data in a computer are sorting and searching. This was initially confirmed by the computer industry. This means that sorting and searching are also the two most worthwhile operations in the computer science field. These simple algorithms are insertion sorting algorithms, Bubble sorting algorithms, and selection sorting algorithms. Each of these algorithms is easy to understand and implement. In any case, these algorithms are not the best comprehensive algorithms, but they are the best available algorithms for a small amount of data sets or other special cases.

1. Bubble sorting. Bubble Sorting is one of the slowest available sorting algorithms, but it is also one of the easiest to understand and implement. This sort algorithm is named after it because the value "like a bubble" floats from one end of the sequence to the other. Assume that you want to sort the number of columns in ascending order, that is, a large value floats to the right of the column, while a small value floats to the left of the column. This effect can be achieved through the following operations: traverse the entire column multiple times and compare adjacent values. If the value on the left is greater than the value on the right, exchange.

public void BubbleSort(){      int temp;      for (int outer = upper; outer >= 1; outer--)      {            for (int inner = 0; inner <= outer - 1; inner++)            {                  if ((int)arr[inner] > arr[inner + 1])                  {                        emp = arr[inner];                        arr[inner] = arr[inner + 1];                        arr[inner + 1] = temp;                  }            }      }}

2. Select sort. This sort starts from the beginning of the array and compares the first element with other elements in the array. Then, place the smallest element in The 0th positions, and then sort the elements again from the 1st positions. This operation will not end until every element except the last element acts as the starting point of the new loop. Two layers of loops are used in the selection sorting algorithm. The outer loop moves from the first element of the array to the element before the last element of the array, while the inner loop moves from the second element of the array to the last element of the array, and find a value smaller than the element in the current outer loop. After traversing through the internal loop, the minimum value in the array is assigned to the appropriate position in the array.

public void SelectionSort(){        int min, temp;      for (int outer = 0; outer <= upper; outer++)      {            min = outer;            for (int inner = outer + 1; inner <= upper; inner++)            {                  if (arr[inner] < arr[min]) min = inner;            }            temp = arr[outer];            arr[outer] = arr[min];            arr[min] = temp;      }}

3. Insert sorting. The insert sorting algorithm is similar to the method that people usually sort by number or letter. If I ask the class to fill in an index card with my name, student ID, and brief self-introduction. The cards handed back by the students are arranged randomly. If you want to sort the cards in alphabetical order, you can create a seat table. The insertion sorting algorithm has two loops. The outer loop traverses array elements one by one, while the inner loop compares the elements selected by the outer loop with the next element in the array. If the element selected by the outer loop is smaller than the element selected by the inner loop, the array elements are shifted to the right to leave a position for the inner loop element.

public void InsertionSort(){      int inner, temp;      for (int outer = 1; outer <= upper; outer++)      {            temp = arr[outer];            inner = outer;            while (inner > 0 && arr[inner - 1] >= temp)            {                  arr[inner] = arr[inner - 1];                  inner -= 1;            }            arr[inner] = temp;      }}

The complexity of the above three sort algorithms is very similar to that of the theory. Therefore, when comparing the three sort algorithms with each other, the operation should be similar. (Test method omitted) the execution efficiency of the selected sorting and Bubble Sorting is equal, the insert sorting speed is half the speed of the other two algorithms (or twice the speed of the other algorithms ). When the number of array elements is 1000, the sorting method is more than 100 times faster than the Bubble sorting method, and the sorting method is more than 200 times faster than the insert sorting method. When the number of array elements is increased to 10000, we can see the effect of array size on the three sorting algorithms. Although the selected sorting algorithm is always many times faster than the other two algorithms, the performance of all these three sorting algorithms is still quite low. Accurately speaking, none of these algorithms are ideal for sorting large data sets. However, there are sorting algorithms that can efficiently process large data sets.

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.