Python implementations of several sorting algorithms

Source: Internet
Author: User

Several sorting algorithms

Several sorting algorithms
    • Several sorting algorithms
          • Bubble sort
          • Select sort
          • Insert Sort
          • Fast sorting Quick Sort
Bubble sort

Bubble sort is a relatively simple sorting method, and the idea is to repeat the sequence to be sorted, compare two elements at a time, and if the order is wrong, swap the position of the element until no element needs to be swapped.

Original 6 1 8 5 9 7
First time 1 6 8 5 9 7
Second time 1 6 8 5 9 7
Third time 1 6 5 8 9 7
four times 1 6 5 8 9 7
five times 1 6 5 8 7 9

......

Nth
Time 1 5 6 7 8 9

Code implementation:

1.LST = [6,1,8,5,9,7]
2. forIinchRange (len (LST)-1):
3. forJinchRange (len (LST)-1):
4. ifLST[J] > lst[j+1]:
5.TMP = Lst[j]
6.LST[J] = lst[j+1]
7.lst[j+1] = tmp
8.PrintJs2
Select sort

Select Sort is to select a minimum (or maximum) element from the array waiting to be sorted, and take it out into the new array until all the elements are removed.

Original 6 1 8 5 9 7 3
First time 1 6 8 5 9 7 3
Second time 1 3 8 5 9 7 6
Third time 1 3 5 8 9 7 6
four times 1 3 5 6 9 7 8
five times 1 3 5 6 7 9 8
six times 1 3 5 6 7 8 9

The smallest array after each order is placed at the end of the sorted sequence

Implementation code

1.LST = [6,1,8,5,9,7,3]
2. forIinchRange (len (LST)):
3.TMP = Lst[i]
4.pos = i
5. forJinchRange (i+1, Len (LST)):
6. iftmp > LST[J]:
7.TMP = Lst[j]
8.pos = J
9.A_tmp = Lst[i]
.Lst[i] = tmp
One by one .Lst[pos] = a_tmp
.
.PrintJs2
Insert Sort

Insertion sorting is a simple and intuitive sorting algorithm, which is constructed by constructing an ordered sequence that, for unsorted data, is scanned in a sorted sequence from a post-think point, and then inserted after the corresponding position is found. The insertion sort is usually sorted by In-place, which requires only an extra space of O (1).

Algorithm Description:

    1. Starting with the first element, remembering that the element is sorted,
    2. Gets the next element as a new element, in the sorted sequence, from the back to the forward scan
    3. If the element (the element in the sorted sequence) is greater than the new element, move the element to the next position
    4. Repeat step 3 until you find the sorted element drizzle or equal to the position of the new element
    5. Insert a heart element into the position
    6. Repeat step 2 ~ Step 5 until the sort is complete

When sorting, if the operation cost of the element comparison is relatively large, you can use a binary lookup to reduce the operation.
Implementation code:

1.LST = [6,1,8,5,9,7,2,4,6,9,2, +, $, the,8,3,3,4]
2.
3. forIinchRange (len (LST)):
4. forJinchRange (I,0,-1):
5. iflst[j-1] >lst[j]:
6.TMP = lst[j-1]
7.lst[j-1] = Lst[j]
8.LST[J] = tmp
Fast sorting Quick Sort

Also known as the division of Exchange sorting; ordering n elements requires 0 (n log n) comparisons, worst case, 0 (N2) comparisons, but this is not a common situation.

With sequence LST = [3,0,1,8,7,2,5,4,9,6], i= 0 j=9 k = lst[0]

5 tr>
0 1 2 3 4 5 6 7 8 9
3 0 1 8 7 2 5 4 9 6
2 0 1 8 7 3 5 4 9 6
2 0 1 3 7 8 5 4 9 6
2 0 1 3 7 8 4 9 6
    1. With 3 as the benchmark, the right-to-left look for a value smaller than 3, J –
    2. Find the first number less than three 2, Lst[i] lst[j] swap position;
    3. Right-to-left looking for number 8, interchange Lst[i] lst[j] position of the first greater than 3 i++
      1. Continue to find a number smaller than 3 from right to left until I==j is completed at this time, and the current position of 3 is the correct position
    4. At this point the large sequence can be divided into two small sequences
      SUB_LST_01 = [2,0,1]
      SUB_LST_01 = [7,8,5,4,9,6]
      The two subsequence is sorted according to the rules of the first trip, until all subsequence lengths are 1.
2 0 1
1 0 2

Based on 2, the right-to-left look for a value smaller than 2, find the number 1:2 small, swap the two positions
At this point from left to right looking for a number larger than 2, not found, 2 of the position is the correct position after sorting

1 0
0 1

Based on 1, right-to-left looking for a number smaller than 1, find 0:1 small, swap the two positions
from left to right to find a number larger than 1, not found, 1 position is the correct position after sorting
0|
-|
The sequence is only 0, the length is 1, and the position of 0 is the correct position after sorting

7 td>
7 8 5 4 9 6
6 8 5 4 9 7
6 7 5 4 9 8
6 4 5 9 8
. . . . . .
5 4 6 . 8 9
5 4 . . . .
4 5 6 7 8 9
    1. With 7 as the benchmark, the right-to-left is found to be 7 smaller than the number 6, swapping the two positions
    2. Find from left to right up to 7 large number 8 interchange two positions
    3. From right to left to find a number 4 smaller than 7, swap two positions
    4. Look from left to right to a number larger than 7, not found, 7 is in the correct position after sorting
      Produces two sub-sequences [6,4,5] [8,9]

Repeat until all sequencing is complete.

Python implementations of several sorting 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.