Various sorting algorithms Python and Java implementations (i)

Source: Internet
Author: User

Start with the simplest implementation of bubble sort:

#-*-Coding:utf-8-*-intarray=[3,4,5,1,2,0,6,9,7]def Bubble (array): For    I in range (1,len (array)): For    J in rang E (i):    if ARRAY[J] > Array[i]:    array[j],array[i] = array[i],array[j] #遍历数组def print_list (array): For    I in Intarray:        Print I, #执行排序bubble (intarray) #打印print_list (Intarray);


Save as buble.py, command-line mode to run Python directly buble.py

Package Sort;public class Sort {public static void main (string[] args) {int[] array = new int[] {3, 4, 5, 1, 2, 0, 6, 9, 7};bubblesort (Array);p rint (array);} private static void print (int[] array) {for (int i = 0; i < Array.Length; i++) {System.out.print (array[i]);}} private static void Bubblesort (int[] array) {//bubbling of the number of trips, for length-1for (int i = 1; i < Array.Length; i++) {//second layer loop for Each loop required Number of times to bubble for (int j = 0; J < i; J + +) {if (Array[j] > Array[i]) {int temp = Array[j];array[j] = array[i];array[i] = Temp ;}}}}}

After watching the bubble sort, look at a little bit of thinking about the algorithm, the direct insertion of the sort, the algorithm is suitable for sorting the set part of order, first give the implementation of Java:

/** * Assuming that a subset of the current is in order, selecting the next value to insert into this ordered subset of the entire process is primarily to find the subscript to be inserted. The insertion sort is very fast in the case of partial subsets ordered. * Initial Assumptions array[0] ordered * @param array */private static void Insertsort (int[] array) {//default first is ordered int length = Array.length;int te Mp;int j;//inserts the current value into an ordered subset for (int i = 1; i < length; i++) {//If the current value is smaller than the maximum value of an ordered subset, perform an interchange if (Array[i] < array[i-1]) {//TE The MP stores the value currently being inserted, also known as sentinel temp = array[i];//ordered subset from 0-J, backward forward traversal for (j = i-1; J >= 0; j--) {if (temp < array[j]) {//If Sentinel value is less than subset of Current value, the current value of the subset is moved back Array[j + 1] = array[j];} else {//otherwise find Sentinel to insert the subscript break;}} The inserted subscript is J+1array[j + 1] = temp;}}}

The following is the implementation of Python:

def insertsort (array): length = len (array) for I in Range (1,length): if Array[i] < array[i-1]:temp=array[i]for j in range ( 0,i) [:: -1]:if temp < array[j]:array[j+1] = Array[j]else:j = J+1breakarray[j] = Temp


After reading the direct sorting, and then look at the selection of sorting, as the name implies, select the minimum value of the subset each time, see Java implementation:

/** * Select Sort, select the minimum value of the subset per trip, then swap *  * @param array */public static void Selectionsort (int[] array) {int temp, minindex;for ( int i = 0; i < Array.Length; i++) {//assuming the initial minimum value is the first element of a subset Minindex = i;for (int j = i + 1; j < Array.Length; J + +) {if (Array[minindex] > Array[j]) {/ /Find the lowest value subscript minindex = j;}} if (minindex! = i) {//Interchange temp = array[i];array[i] = Array[minindex];array[minindex] = temp;}}}

Then look at the python version:

def selectionsort (array): For I in Xrange (0,len (array)): index = IFOR J in xrange (I,len (array)): if Array[index] > Array[j ]:index = Jtemp = Array[i]array[i] = Array[index]array[index] = Temp


The above is a simple sort of the Java and Python implementation, the next one to explain a few slightly more complex points of the deformation sort.



Various sorting algorithms Python and Java implementations (i)

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.