Sort Algorithm Part-2 Insertion Sort
2006-03-09 12:39
441 查看
Sort Algorithm
Part -2
Insertion 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. 5
2. def.h
3. sort.h
4. sort.c
#include <string.h>
#include "sort.h"
int main(int argc, char **argv) {
Item item[] = {"ASORTINGEXAMPLE"};
i_sort(item, 0, strlen(item));
return 0;
}
#define less(a, b) ((a) < (b))
#define exch(a, b) {Item tmp; tmp = (a); (a) = (b); (b) = tmp;}
#define compexch(a, b) {if (less((a), (b))) exch((a), (b))}
void i_sort(Item*, int, int);
void i_sort(Item*, int, int);
void i_sort(Item item[], int l, int r) {
int i;
for (i = r - 1; i > l; i--)
compexch(item[i], item[i - 1]);
for (i = l + 2; i < r; i++) {
int j = i;
Item c = item[i];
while (less(c, item[j - 1])) {
item[j] = item[j - 1];
j--;
}
if (j < i)
item[j] = c;
}
}
2. 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.InsertionSort(charItem, null, 0, charItem.Length);
Sorter.InsertionSort(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 InsertionSort(
IList item, IComparer comparer,
int l, int r) {
if (null == comparer)
comparer = Comparer.Default;
for (int i = r - 1; i > l; i--)
if (comparer.Compare(item[i], item[i - 1]) < 0) {
object tmp = item[i];
item[i] = item[i - 1];
item[i - 1] = tmp;
}
for (int i = l + 2; i < r; i++) {
object c = item[i];
int j = i;
while (comparer.Compare(c, item[j - 1]) < 0) {
item[j] = item[j - 1];
j--;
}
if (j < i)
item[j] = c;
}
}
}
}
Part -2
Insertion 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. 5
1. Definition
A sorting algorithm that inserts each item in the proper place into an initially empty list by comparing it with each item in the list until it finds the new element's successor or the end of the list.2. Implements in ANSI C
2.1 List of c files
1. main.c2. def.h
3. sort.h
4. 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 item[] = {"ASORTINGEXAMPLE"};
i_sort(item, 0, strlen(item));
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;}
#define compexch(a, b) {if (less((a), (b))) exch((a), (b))}
2.2.3 sort.h
#include "def.h"void i_sort(Item*, int, int);
2.2.4 sort.c
#include "sort.h"void i_sort(Item*, int, int);
void i_sort(Item item[], int l, int r) {
int i;
for (i = r - 1; i > l; i--)
compexch(item[i], item[i - 1]);
for (i = l + 2; i < r; i++) {
int j = i;
Item c = item[i];
while (less(c, item[j - 1])) {
item[j] = item[j - 1];
j--;
}
if (j < i)
item[j] = c;
}
}
3. Implements in C#
3.1 List of c# files
1. main.cs2. sorter.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.InsertionSort(charItem, null, 0, charItem.Length);
Sorter.InsertionSort(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 InsertionSort(
IList item, IComparer comparer,
int l, int r) {
if (null == comparer)
comparer = Comparer.Default;
for (int i = r - 1; i > l; i--)
if (comparer.Compare(item[i], item[i - 1]) < 0) {
object tmp = item[i];
item[i] = item[i - 1];
item[i - 1] = tmp;
}
for (int i = l + 2; i < r; i++) {
object c = item[i];
int j = i;
while (comparer.Compare(c, item[j - 1]) < 0) {
item[j] = item[j - 1];
j--;
}
if (j < i)
item[j] = c;
}
}
}
}
相关文章推荐
- Algorithm--Insertion Sort
- Insertion sort algorithm
- Sort Algorithm Part-1 Selection Sort
- Introduction to Algorithm - Summary of Chapter 2(1) - Insertion sort
- Algorithm backup ---- Insertion Sort(插入排序算法)
- Algorithm_1_Insertion_Sort
- Insertion Sort - Part 2
- One Algorithm A Day --- INSERTION-SORT--算法入门
- Hackerrank && Insertion Sort - Part 1
- [LeetCode OJ] Insertion Sort List 解题报告
- counting sort (计数排序) algorithm
- Insertion Sort
- The Solution to LeetCode 147 Insertion Sort List
- Insertion Sort List
- Algorithm: Quick Sort Mind and Related Questions
- Coursera Algorithms, Part 1 Week 1: Analysis of Algorithm
- Insertion Sort List
- Insertion Sort List--leetcode
- insertion-sort-list
- insertion-sort-list