Sort Algorithm Part-1 Selection Sort
2006-03-08 14:06
399 查看
Sort Algorithm
Part -1
Selection Sort
1. Definition. 3
2. Implements in ANSI C. 3
2.1 List of c files. 3
2.2 Source code. 3
2.2.1 main.c 3
2.2.2 def.h. 3
2.2.3 sort.h. 3
2.2.4 sort.c 3
3. Implements in C#. 4
3.1 List of C# files. 4
3.2 Source code. 4
3.2.1 Main.cs. 4
3.2.2 Sorter.cs. 4
def.h
sort.h
sort.c
#include <string.h>
#include "sort.h"
int main(int argc, char **argv) {
Item itm[] = {"ASORTINGEXAMPLE"};
s_sort(itm, 0, strlen(itm));
return 0;
}
#define less(a, b) ((a) < (b))
#define exch(a, b) {Item tmp; tmp = (a); (a) = (b); (b) = tmp;}
void s_sort(Item*, int, int);
void s_sort(Item*, int, int);
void s_sort(Item item[], int l, int r) {
int i;
for (i = l; i < r - 1; i++) {
int min = i, j;
for (j = i + 1; j < r; j++) {
if (less(item[j], item[min]))
min = j;
}
exch(item[i], item[min]);
}
}
Sorter.cs
using System.Collections;
namespace AlgorithmPractice {
/// <summary>
/// Summary description for TestClass.
/// </summary>
class TestClass {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
char[] charItem = {'A', 'S', 'O', 'R', 'T',
'I', 'N', 'G', 'E', 'X',
'A', 'M', 'P', 'L', 'E'};
int[] intItem = new int[]{1, 5, 3, 6, 10,
55, 9, 2, 87, 12,
34, 75, 33, 47, 1};
Sorter.SelectionSort(charItem, null, 0, charItem.Length);
Sorter.SelectionSort(intItem, null, 0, intItem.Length);
System.Diagnostics.Debug.WriteLine(new string(charItem));
}
}
}
using System.Collections;
namespace AlgorithmPractice
{
/// <summary>
/// Summary description for Sorter.
/// </summary>
public class Sorter
{
private Sorter(){}
public static void SelectionSort(
IList item, IComparer comparer,
int l, int r) {
if (null == comparer)
comparer = Comparer.Default;
for (int i = l; i < r - 1; i++) {
int min = i;
object tmp;
for (int j = i + 1; j < r; j++)
if (comparer.Compare(item[j], item[min]) < 0)
min = j;
tmp = item[i];
item[i] = item[min];
item[min] = tmp;
}
}
}
}
Part -1
Selection Sort
Table of Contents
Table of Contents. 21. Definition. 3
2. Implements in ANSI C. 3
2.1 List of c files. 3
2.2 Source code. 3
2.2.1 main.c 3
2.2.2 def.h. 3
2.2.3 sort.h. 3
2.2.4 sort.c 3
3. Implements in C#. 4
3.1 List of C# files. 4
3.2 Source code. 4
3.2.1 Main.cs. 4
3.2.2 Sorter.cs. 4
1. Definition
A sorting technique that is typically used for sequencing small lists. It starts by comparing the entire list for the lowest item and moves it to the #1 position. It then compares the rest of the list for the next-lowest item and places it in the #2 position and so on until all items are in the required order. Selection sorts perform numerous comparisons, but fewer data movements than other methods.2. Implements in ANSI C
2.1 List of c files
main.cdef.h
sort.h
sort.c
2.2 Source code
2.2.1 main.c
#include <stdio.h>#include <string.h>
#include "sort.h"
int main(int argc, char **argv) {
Item itm[] = {"ASORTINGEXAMPLE"};
s_sort(itm, 0, strlen(itm));
return 0;
}
2.2.2 def.h
typedef char Item, *PItem;#define less(a, b) ((a) < (b))
#define exch(a, b) {Item tmp; tmp = (a); (a) = (b); (b) = tmp;}
2.2.3 sort.h
#include "def.h"void s_sort(Item*, int, int);
2.2.4 sort.c
#include "sort.h"void s_sort(Item*, int, int);
void s_sort(Item item[], int l, int r) {
int i;
for (i = l; i < r - 1; i++) {
int min = i, j;
for (j = i + 1; j < r; j++) {
if (less(item[j], item[min]))
min = j;
}
exch(item[i], item[min]);
}
}
3. Implements in C#
3.1 List of C# files
Main.csSorter.cs
3.2 Source code
3.2.1 Main.cs
using System;using System.Collections;
namespace AlgorithmPractice {
/// <summary>
/// Summary description for TestClass.
/// </summary>
class TestClass {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
char[] charItem = {'A', 'S', 'O', 'R', 'T',
'I', 'N', 'G', 'E', 'X',
'A', 'M', 'P', 'L', 'E'};
int[] intItem = new int[]{1, 5, 3, 6, 10,
55, 9, 2, 87, 12,
34, 75, 33, 47, 1};
Sorter.SelectionSort(charItem, null, 0, charItem.Length);
Sorter.SelectionSort(intItem, null, 0, intItem.Length);
System.Diagnostics.Debug.WriteLine(new string(charItem));
}
}
}
3.2.2 Sorter.cs
using System;using System.Collections;
namespace AlgorithmPractice
{
/// <summary>
/// Summary description for Sorter.
/// </summary>
public class Sorter
{
private Sorter(){}
public static void SelectionSort(
IList item, IComparer comparer,
int l, int r) {
if (null == comparer)
comparer = Comparer.Default;
for (int i = l; i < r - 1; i++) {
int min = i;
object tmp;
for (int j = i + 1; j < r; j++)
if (comparer.Compare(item[j], item[min]) < 0)
min = j;
tmp = item[i];
item[i] = item[min];
item[min] = tmp;
}
}
}
}
相关文章推荐
- Sort Algorithm Part-2 Insertion Sort
- Algorithm: Quick Sort Mind and Related Questions
- 要找重复的串, 好麻烦, 还是写个程序来搞吧---利用algorithm对vector<string>进行sort
- CNTK学习笔记 -- Computation Network part 1 -- Forward and Backward Algorithm
- Algorithm, Part I Princeton University 公开课第一周 Union-Find算法
- Week2 Assignment - Quicksort - Stanford - Algorithm: Design and Analysis Part I
- Sort algorithm overview.
- Algorithm | Sort
- 选择排序(Selection Sort)和插入排序(Insertion Sort)
- Algorithm -- Selection Sort
- RTB撕开黑盒子 Part 2: Algorithm Meets World
- Algorithm——MergeSort
- 《PCL点云库学习&VS2010(X64)》Part 45 点云压缩算法—扫描线(DouglasPeuckerAlgorithm)
- #include <algorithm>中sort的一般用法
- Algorithm backup ---- Insertion Sort(插入排序算法)
- Algorithm Part I:Queues,Stacks
- Algorithm: Quick Sort Mind and Related Questions
- STL:algorithm中排序函数sort(升序排列函数)和reverse(反转排列函数)的简单用法
- Select Sort Algorithm
- #include <algorithm>中sort的一般用法