Sorting algorithm--direct selection of sorting

Source: Internet
Author: User

One, C program implementation

/******************************************************** Description: Direct Select sort * author:shujuxiong* version:1.0* time : 2018-06-24*******************************************************/#include <stdio.h>//function: print array void Printdataarray (int a[], int n) {    for (int i=0; i<n; i++)        printf ("%d", A[i]);    printf ("\ n");} void Selectsort (int a[], int n) {for    (int i = 0; i < n; i++)    {        int min = i;        for (int j = i+1; J < N; j + +)        {            if (A[j] < a[min])                    min = j;        }        int tmp = A[i];        A[i] = a[min];        A[min] = tmp;    }    Printdataarray (A, n);} Test Case int Main () {    int a[] = {3,1,7,5,2,4,9,6};    int len = sizeof (a)/sizeof (a[0]);    Selectsort (A, Len);    return 0;}

Operation Result:

Second, Java program implementation

/** * @description: Direct selection sorting algorithm * @author: Shujuxiong * @version: 1.0 * @date: 2018-06-24 */public class Selectsort {public S tatic void sort (int[] a) {int N = a.length;for (int i = 0; i < N; i++) {int min = i;for (int j = i+1; J < N; J + +) {if (a[j ] < A[min]) min = j;} int tmp = A[i];a[i] = a[min];a[min] = tmp;}} Method: Print array private static void Printdataarray (int[] a) {int N = a.length;for (int i = 0; i < N; i++) System.out.printf ("%d" , A[i]); System.out.printf ("\ n");} Test case public static void main (string[] args) {int[] arr = {3,1,7,5,2,4,9,6};sort (arr);p Rintdataarray (arr);}

Operation Result:

Third, the Python program implementation

#-*-Coding:utf-8-*-"" "Description: Direct selection sorting algorithm author:shujuxiongversion:1.0date:2018-06-24" "Import copy# #选择排序def Selectsort (relist):    N = len (relist) for    I in range (N):        min = i;        For j in Range (I+1,n):            if RELIST[J] < Relist[min]:                min = j        tmp = Relist[i];        Relist[i] = Relist[min]        relist[min] = tmp        return relist# #测序用例def main ():    mylist = [3,1,7,5,2,4,9,6]    Print (Selectsort (copy.copy (mylist))) if __name__== ' __main__ ':    Main ()

Operation Result:

Sorting algorithm--direct selection of sorting

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.