Implementation of various sorting algorithms in python and java (1)

Source: Internet
Author: User

Implementation of various sorting algorithms in python and java (1)

First, Implement Bubble Sorting in the simplest way:

#-*-Coding: UTF-8-*-intarray = [, 7] def bubble (array): for I in range (1, len (array): for j in range (I): if array [j]> array [I]: array [j], array [I] = array [I], array [j] # traverse the array def print_list (array): for I in intarray: print I, # execute the sort bubble (intarray) # print print_list (intarray );


Stored as buble. py. Run python buble. py directly in command line mode.

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); print (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) {// Number of bubbles, length-1for (int I = 1; I <array. length; I ++) {// The second-level loop is the number of bubbles required for each loop. for (int j = 0; j <I; j ++) {if (array [j]> array [I]) {int temp = array [j]; array [j] = array [I]; array [I] = temp ;}}}}}

After reading the Bubble sorting, you can look at an algorithm that is a little bit of thinking and insert the sorting directly. This algorithm is suitable for sorting the partial order of the set to be sorted. The java Implementation method is given first: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;">/*** assume that the current subset is ordered, the whole process of selecting the next value to insert into this ordered subset is mainly to find the subscript to insert. Insert sorting is very fast when partial subsets are sorted. * Initially assume that array [0] is ordered * @ param array */private static void insertSort (int [] array) {// by default, the first one is ordered int length = array. length; int temp; int j; // Insert the current value to the sorted subset for (int I = 1; I <length; I ++) {// if the current value is smaller than the maximum value of the ordered subset, execute if (array [I] <array [I-1]) {// temp to store the current value to be inserted, it is also called the Sentinel temp = array [I]; // The sorted subset is traversed from 0-j and forward from behind (j = I-1; j> = 0; j --) {if (temp <array [j]) {// if the Sentinel value is smaller than the current value of the subset, the current value of the subset is moved back to array [j + 1] = array [j];} else {// otherwise, the subscript break to be inserted by the Sentinel is found ;}} // The inserted subscript is j + 1 array [j + 1] = temp ;}}}

The python implementation is as follows:

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, let's look at the selection of sorting. As the name suggests, we select sorting. Each traversal selects the minimum value of the subset. Let's look at the java implementation:

/*** Select sorting, select the minimum value of the subset for each trip, and then switch ** @ param array */public static void selectionSort (int [] array) {int temp, minIndex; for (int I = 0; I <array. length; I ++) {// assume that the initial minimum value is the first element of the subset, minIndex = I; for (int j = I + 1; j <array. length; j ++) {if (array [minIndex]> array [j]) {// find the subscript minIndex = j;} if (minIndex! = I) {// exchange temp = array [I]; array [I] = array [minIndex]; array [minIndex] = temp ;}}}

Let's take a 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 the java and python implementation of centralized and simple sorting. The next article explains how to perform deformation sorting for a few slightly complex points.



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.