# 算法 - 排序 - 选择排序

C++代码:

Sorter.hpp

#ifndef _Algorithm_Sorter_H_
#define _Algorithm_Sorter_H_

template <typename Item>
class Sorter
{
public:
static void selectionSort(Item a[], int l, int r);
static void show(Item a[], int length);
private:
static void exch(Item &A, Item &B);
};

template <typename Item>
void Sorter<Item>::show(Item a[], int length)
{
for (int i = 0; i < length; i++)
cout << a[i] << " ";
cout << endl;
}

template <typename Item>
void Sorter<Item>::exch(Item &A, Item &B)
{
Item t = A; A = B; B = t;
}

template <typename Item>
void Sorter<Item>::selectionSort(Item a[], int l, int r)
{
for (int i = l; i < r; i++)
{
int min = i;
for (int j = 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"

class SorterTest : public TestBase
{
public:
SorterTest(const string &c, const string &d) : TestBase(c, d) { }
void run();
private:
void selectionSortTest();
};

#endif

SorterTest.cpp

#include <vector>
#include <iostream>
#include "SorterTest.h"
#include "Sorter.hpp"

using namespace std;

void SorterTest::selectionSortTest()
{
int *a = new int[10];
for (int i = 0; i < 10; i++)
a[i] = 1000 * (1.0*rand() / RAND_MAX);

cout << "before sorting..." << endl;
Sorter<int>::show(a, 10);

Sorter<int>::selectionSort(a, 0, 9);

cout << "after sorting..." << endl;
Sorter<int>::show(a, 10);
}

void SorterTest::run()
{
printStart("selectionSortTest()");
selectionSortTest();
printEnd("selectionSortTest()");
}

---------------- 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代码:

package zeus.algorithm.sort;

public class Sorter {
public static void selectionSort(Comparable[] a) { // Sort a[] into increasing order.
int N = a.length; // array length
for (int i = 0; i < N; i++) { // Exchange a[i] with smallest entry in
// a[i+1...N).
int min = i; // index of minimal entr.
for (int j = i + 1; j < N; j++)
if (less(a[j], a[min]))
min = j;
exch(a, i, min);
}
}

private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}

private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}

private static void show(Comparable[] a) { // Print the array, on a single
// line.
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}

private static boolean isSorted(Comparable[] a) {
return isSorted(a, 0, a.length - 1);
}

//is the array sorted from a[lo] to a[hi]
private static boolean isSorted(Comparable[] a, int lo, int hi) {
for (int i = lo + 1; i <= hi; i++)
if (less(a[i], a[i - 1]))
return false;
return true;
}

public static void main(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);
}

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

posted @ 2015-09-29 20:15  Master HaKu  阅读(188)  评论(0编辑  收藏