Algorithm-sort-select sort

Source: Internet
Author: User
Tags comparable

Each cycle moves the smallest value forward.

C + + code:

Sorter.hpp

#ifndef _algorithm_sorter_h_#define_algorithm_sorter_h_Template<typename item>classsorter{ Public:    Static voidSelectionsort (Item a[],intLintR); Static voidShow (Item a[),intlength);Private:    Static voidExch (Item &a, Item &B);}; Template<typename item>voidSorter<item>::show (Item a[],intlength) {     for(inti =0; i < length; i++) cout<< A[i] <<" "; cout<<Endl;} Template<typename item>voidSorter<item>::exch (item &a, Item &c) {Item T= A; A = B; B =T;} Template<typename item>voidSorter<item>::selectionsort (Item a[],intLintR) {     for(inti = l; I < R; i++)    {        intMin =i;  for(intj = i +1; J <= R; J + +)        if(A[j] < a[min]) min =J;    Exch (A[i], a[min]); }}#endif

SorterTest.h

#ifndef _algorithm_sorter_test_h_#define_algorithm_sorter_test_h_#include".. /testbase.h"classSortertest: Publictestbase{ Public: Sortertest (Const string&c,Const string&d): Testbase (c, D) {}voidrun ();Private:    voidselectionsorttest ();};#endif

SorterTest.cpp

#include <vector>#include<iostream>#include"SorterTest.h"#include"sorter.hpp"using namespacestd;voidsortertest::selectionsorttest () {int*a =New int[Ten];  for(inti =0; I <Ten; i++) A[i]= +* (1.0*rand ()/Rand_max); cout<<"before sorting ..."<<Endl; Sorter<int>::show (A,Ten); Sorter<int>::selectionsort (A,0,9); cout<<"After sorting ..."<<Endl; Sorter<int>::show (A,Ten);}voidSortertest::run () {Printstart ("selectionsorttest ()");    Selectionsorttest (); Printend ("selectionsorttest ()");}

Operation Result:
----------------selectionsorttest (): Run Start----------------
Before sorting ...
1 563 193 808 585 479 350 895 822 746
After sorting ...
1 193 350 479 563 585 746 808 822 895
----------------selectionsorttest (): Run End----------------

Java code:

 PackageZeus.algorithm.sort; Public classSorter { Public Static voidSelectionsort (comparable[] a) {//Sort a[] into increasing order.        intN = A.length;//Array Length         for(inti = 0; i < N; i++) {//Exchange A[i] with smallest entry in//A[I+1...N).            intmin = i;//index of minimal entr.             for(intj = i + 1; J < N; J + +)                if(Less (a[j], a[min)) min=J;        Exch (A, I, min); }    }        Private Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) < 0; }    Private Static voidExch (comparable[] A,intIintj) {comparable T=A[i]; A[i]=A[j]; A[J]=T; }    Private Static voidShow (comparable[] a) {//Print the array, on a single //Line .         for(inti = 0; i < a.length; i++) System.out.print (A[i]+ " ");    System.out.println (); }    Private Static Booleanissorted (comparable[] a) {returnIsSorted (A, 0, a.length-1); }//is the array sorted from A[lo] to A[hi]    Private Static BooleanIsSorted (comparable[] A,intLointhi) {         for(inti = lo + 1; I <= hi; i++)            if(Less (A[i], a[i-1]))                return false; return true; }         Public Static voidMain (string[] args) {System.out.println ("********* Selection Sort *********"); String[] a= {"23", "12", "9", "1", "8", "20" };        Selectionsort (a); System.out.println ("Sort by String:");                Show (a); System.out.println ("--------------------------------------------"); Integer[] B= {23, 12, 9, 1, 8, 20 };        Selectionsort (b); System.out.println ("Sort by Integer:"); Show (b);}

Operation Result:

Selection Sort *********
Sort by String:
1 12 20 23 8 9
--------------------------------------------
Sort by Integer:
1 8 9 12 20 23

Algorithm-sort-select sort

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.