Python to Implement Bubble, insert, select simple sort instance, python bubble

Source: Internet
Author: User

Python to Implement Bubble, insert, select simple sort instance, python bubble

The Python implementation in this article bubble, insert, and simple sorting examples are suitable for beginners of Python to learn data structures and algorithms from the basics. The example is simple and easy to understand. The specific code is as follows:

#-*-Coding: cp936-*-# insert sort def insertSort (a): for I in range (len (a)-1): # print, I for j in range (I + 1, len (a): if a [I]> a [j]: temp = a [I] a [I] = a [j] a [j] = temp return a # Python's Bubble Sorting def bubbleSort (alist ): for passnum in range (len (alist)-1, 0,-1): # print alist, passnum for I in range (passnum ): if alist [I]> alist [I + 1]: temp = alist [I] alist [I] = alist [I + 1] alist [I + 1] = temp return alist # select and sort def selectionSort (alist) in Python ): for I in range (len (alist)-1, 0,-1): maxone = 0 for j in range (1, I + 1 ): if alist [j]> alist [maxone]: maxone = j temp = alist [I] alist [I] = alist [maxone] alist [maxone] = temp return alistalist = [54,26, 93,17, 77,31, 44,55, 20] # print bubbleSort (alist) alist = [54,26, 93,17, 77,31, 44,55, 20] print selectionSort (alist)

If you are interested, you can test the examples in this article. I believe there will be new gains.


Select sorting, fast sorting, Bubble sorting, heap sorting, insert sorting, and the running speed of the basic sorting program.

It depends on time complexity.
O (n ^ 2), O (n ^ 2), O (n ^ 2), O (nlog 2 n), O (nlog 2 n ), O (nlog 2 n ),

Write sorting algorithms, simple selection of sorting, direct insertion of sorting, Bubble sorting, details

I know this and hope to help you!
/* Sort */
# Include "stdio. h"
# Define M 50

Void bubblesort (int a [], int n );
Void insertsort (int a [], int n );
Void selectsort (int a [], int n );
Void main ()
{
Int a [M], I, n, j, locate, kk;
Clrscr ();
While (1)
{
Window (,); clrscr ();
Printf ("\ n ---------------------------------- \ n ");
Printf ("1. create 2. bubblesort 3. insertsort 4. selectsort \ n ");
Printf ("0. exit \ n ");
Printf ("input your choice :");
Scanf ("% d", & kk );
Switch (kk)
{
Case 1: window (,); clrscr ();
Printf ("\ ninput number :");
Scanf ("% d", & n );
For (I = 1; I <= n; I ++)
Scanf ("% d", & a [I]);
Printf ("\ n ");
For (I = 1; I <= n; I ++)
Printf ("% 5d", a [I]);
Break;
Case 2: window (,); clrscr ();
Bubblesort (a, n );
Break;

Case 3: window (,); clrscr ();
Insertsort (a, n );
Break;
Case 4: window (,); clrscr ();
Selectsort (a, n );
Break;
Default: exit (0 );
}
}

}
Void selectsort (int a [], int n)/* simple selection and sorting */
{
Int I, j, k, temp, m;
For (I = 1;... the remaining full text>

Related Article

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.