Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,8 +1,13 @@
|
|||
{{Sorting Algorithm}}
|
||||
{{wikipedia|Insertion sort}}
|
||||
An <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> sorting algorithm which moves elements one at a time into the correct position. The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part.
|
||||
{{omit from|GUISS}}
|
||||
An <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> sorting algorithm which moves elements one at a time into the correct position.
|
||||
The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary.
|
||||
To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part.
|
||||
|
||||
Although insertion sort is an <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases (i) small <span style="font-family: serif">''n''</span>, (ii) as the final finishing-off algorithm for <span style="font-family: serif">[[O]](''n'' log''n'')</span> algorithms such as [[Merge sort|mergesort]] and [[quicksort]].
|
||||
Although insertion sort is an <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases: <br>
|
||||
(i) small <span style="font-family: serif">''n''</span>, <br>
|
||||
(ii) as the final finishing-off algorithm for <span style="font-family: serif">[[O]](''n'' log''n'')</span> algorithms such as [[Merge sort|mergesort]] and [[quicksort]].
|
||||
|
||||
The algorithm is as follows (from [[wp:Insertion_sort#Algorithm|wikipedia]]):
|
||||
'''function''' ''insertionSort''(array A)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,26 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
template<typename Iter>
|
||||
void insertion_sort(Iter beg, Iter end)
|
||||
{
|
||||
for (Iter i = beg; i != end; ++i)
|
||||
std::rotate(std::upper_bound(beg, i, *i), i, i+1);
|
||||
template <typename RandomAccessIterator, typename Predicate>
|
||||
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end,
|
||||
Predicate p) {
|
||||
for (auto i = begin; i != end; ++i) {
|
||||
std::rotate(std::upper_bound(begin, i, *i, p), i, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename RandomAccessIterator>
|
||||
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end) {
|
||||
insertion_sort(
|
||||
begin, end,
|
||||
std::less<
|
||||
typename std::iterator_traits<RandomAccessIterator>::value_type>());
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a[] = { 100, 2, 56, 200, -52, 3, 99, 33, 177, -199 };
|
||||
insertion_sort(std::begin(a), std::end(a));
|
||||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,24 @@
|
|||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void insertion_sort(int *a, const size_t n) {
|
||||
size_t i, j;
|
||||
int value;
|
||||
void insertion_sort (int *a, int n) {
|
||||
int i, j, t;
|
||||
for (i = 1; i < n; i++) {
|
||||
value = a[i];
|
||||
for (j = i; j > 0 && value < a[j - 1]; j--) {
|
||||
t = a[i];
|
||||
for (j = i; j > 0 && t < a[j - 1]; j--) {
|
||||
a[j] = a[j - 1];
|
||||
}
|
||||
a[j] = value;
|
||||
a[j] = t;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int main () {
|
||||
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
|
||||
insertion_sort(a, sizeof a / sizeof a[0]);
|
||||
int n = sizeof a / sizeof a[0];
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
insertion_sort(a, n);
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
(defun insertion-sort (sequence &optional (predicate #'<))
|
||||
(if (cdr sequence)
|
||||
(insert (car sequence) ;; insert the current item into
|
||||
(insertion-sort (cdr sequence) ;; the already-sorted
|
||||
predicate) ;; remainder of the list
|
||||
predicate)
|
||||
sequence)) ; a list of one element is already sorted
|
||||
|
||||
(defun insert (item sequence predicate)
|
||||
(cond ((null sequence) (list item))
|
||||
((funcall (complement predicate) ;; if the first element of the list
|
||||
(car sequence) ;; isn't better than the item,
|
||||
item) ;; cons the item onto
|
||||
(cons item sequence)) ;; the front of the list
|
||||
(t (cons (car sequence) ;; otherwise cons the first element onto the front of
|
||||
(insert item ;; the list of the item sorted with the rest of the list
|
||||
(cdr sequence)
|
||||
predicate)))))
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
void insertionSort(T)(T[] data) pure nothrow {
|
||||
foreach (i, value; data[1 .. $]) {
|
||||
void insertionSort(T)(T[] data) pure nothrow @safe @nogc {
|
||||
foreach (immutable i, value; data[1 .. $]) {
|
||||
auto j = i + 1;
|
||||
for ( ; j > 0 && value < data[j - 1]; j--)
|
||||
data[j] = data[j - 1];
|
||||
|
|
@ -10,6 +10,6 @@ void insertionSort(T)(T[] data) pure nothrow {
|
|||
void main() {
|
||||
import std.stdio;
|
||||
auto items = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
|
||||
items.insertionSort();
|
||||
writeln(items);
|
||||
items.insertionSort;
|
||||
items.writeln;
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import std.stdio, std.range, std.algorithm, std.traits;
|
||||
|
||||
void insertionSort(R)(R arr)
|
||||
if (hasLength!R && isRandomAccessRange!R && hasSlicing!R) {
|
||||
foreach (immutable i; 1 .. arr.length)
|
||||
bringToFront(arr[0 .. i].assumeSorted.upperBound(arr[i]), arr[i .. i + 1]);
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.random, std.container;
|
||||
|
||||
auto arr1 = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
|
||||
arr1.insertionSort;
|
||||
assert(arr1.isSorted);
|
||||
writeln("arr1 sorted: ", arr1);
|
||||
|
||||
auto arr2 = Array!int([28, 44, 46, 24, 19, 2, 17, 11, 25, 4]);
|
||||
arr2[].insertionSort;
|
||||
assert(arr2[].isSorted);
|
||||
writeln("arr2 sorted: ", arr2[]);
|
||||
|
||||
// Random data test.
|
||||
int[10] buf;
|
||||
foreach (immutable _; 0 .. 100_000) {
|
||||
auto arr3 = buf[0 .. uniform(0, $)];
|
||||
foreach (ref x; arr3)
|
||||
x = uniform(-6, 6);
|
||||
arr3.insertionSort;
|
||||
assert(arr3.isSorted);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
fn inSort arr =
|
||||
(
|
||||
arr = deepcopy arr
|
||||
for i = 1 to arr.count do
|
||||
(
|
||||
j = i
|
||||
while j > 1 and arr[j-1] > arr[j] do
|
||||
(
|
||||
swap arr[j] arr[j-1]
|
||||
j -= 1
|
||||
)
|
||||
)
|
||||
return arr
|
||||
)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
b = for i in 1 to 20 collect random 1 40
|
||||
#(2, 28, 35, 31, 27, 24, 2, 22, 15, 34, 9, 10, 22, 40, 26, 5, 23, 6, 18, 33)
|
||||
a = insort b
|
||||
#(2, 2, 5, 6, 9, 10, 15, 18, 22, 22, 23, 24, 26, 27, 28, 31, 33, 34, 35, 40)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
insertion_sort <- function(x) {
|
||||
for (j in 2:length(x)) {
|
||||
key <- x[j]
|
||||
bp <- which.max(x[1:j] > key)
|
||||
# 'bp' stands for breakpoint
|
||||
if (bp == 1) {
|
||||
if (key < ar[1]){
|
||||
x <- c(key, ar[-j])
|
||||
}
|
||||
}
|
||||
else {
|
||||
x <- x[-j]
|
||||
x <- c(ar[1:bp - 1], key, x[bp : (s-1)])
|
||||
}
|
||||
return(x)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +1,38 @@
|
|||
/*REXX program sorts a stemmed array using the insertion-sort method.*/
|
||||
/*REXX program sorts a stemmed array using the insertion-sort algoritm.*/
|
||||
call gen@ /*generate the array's elements. */
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call insertionSort highItem /*invoke the insertion sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call insertionSort # /*invoke the insertion sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.= /*assign default value to array. */
|
||||
@.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
|
||||
@.2 = "Monday's child is fair of face;"
|
||||
@.3 = "Tuesday's child is full of grace;"
|
||||
@.4 = "Wednesday's child is full of woe;"
|
||||
@.5 = "Thursday's child has far to go;"
|
||||
@.6 = "Friday's child is loving and giving;"
|
||||
@.7 = "Saturday's child works hard for a living;"
|
||||
@.8 = "But the child that is born on the Sabbath day"
|
||||
@.9 = "Is blithe and bonny, good and gay."
|
||||
do highItem=1 while @.highItem\=='' /*find how many entries in array.*/
|
||||
end /*short and sweet DO loop, eh? */
|
||||
highItem=highItem-1 /*because of DO, adjust highItem.*/
|
||||
gen@: @. = /*assign default value to array. */
|
||||
@.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
|
||||
@.2 = "Monday's child is fair of face;"
|
||||
@.3 = "Tuesday's child is full of grace;"
|
||||
@.4 = "Wednesday's child is full of woe;"
|
||||
@.5 = "Thursday's child has far to go;"
|
||||
@.6 = "Friday's child is loving and giving;"
|
||||
@.7 = "Saturday's child works hard for a living;"
|
||||
@.8 = "But the child that is born on the Sabbath day"
|
||||
@.9 = "Is blithe and bonny, good and gay."
|
||||
|
||||
do #=1 while @.#\=='' /*find how many entries in array.*/
|
||||
end /*#*/ /*short and sweet DO loop, eh? */
|
||||
#=#-1 /*because of DO, adjust # entries*/
|
||||
return
|
||||
/*──────────────────────────────────INSERTIONSORT subroutine────────────*/
|
||||
insertionSort: procedure expose @.; parse arg highItem
|
||||
do i=2 to highItem; value=@.i
|
||||
do j=i-1 by -1 while j\==0 & @.j>value
|
||||
jp=j+1; @.jp=@.j
|
||||
end /*j*/
|
||||
insertionSort: procedure expose @. #
|
||||
do i=2 to #
|
||||
value=@.i; do j=i-1 by -1 while j\==0 & @.j>value
|
||||
jp=j+1; @.jp=@.j
|
||||
end /*j*/
|
||||
jp=j+1
|
||||
@.jp=value
|
||||
end /*i*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: widthH=length(highItem) /*the maximum width of any line. */
|
||||
do j=1 for highItem
|
||||
say 'element' right(j,widthH) arg(1)': ' @.j
|
||||
end /*j*/
|
||||
show@: do j=1 for #
|
||||
say 'element' right(j,length(#)) arg(1)': ' @.j
|
||||
end /*j*/
|
||||
say copies('─',79) /*show a separator line that fits*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,13 +1,9 @@
|
|||
(require racket/match)
|
||||
#lang racket
|
||||
|
||||
(define (sort pred l)
|
||||
(define (sort < l)
|
||||
(define (insert x y)
|
||||
(match (list x y)
|
||||
[(list x '()) (list x)]
|
||||
[(list x (cons y ys))
|
||||
(if (pred x y)
|
||||
(cons x (cons y ys))
|
||||
(cons y (f x ys)))]))
|
||||
(match l
|
||||
['() '()]
|
||||
[(cons x xs) (insert x (sort pred xs))]))
|
||||
(match* (x y)
|
||||
[(x '()) (list x)]
|
||||
[(x (cons y ys)) (cond [(< x y) (list* x y ys)]
|
||||
[else (cons y (insert x ys))])]))
|
||||
(foldl insert '() l))
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
def insert(list: List[Int], value: Int) = list.span(_ < value) match {
|
||||
case (lower, upper) => lower ::: value :: upper
|
||||
def insertSort[X](list: List[X])(implicit ord: Ordering[X]) = {
|
||||
def insert(list: List[X], value: X) = list.span(x => ord.lt(x, value)) match {
|
||||
case (lower, upper) => lower ::: value :: upper
|
||||
}
|
||||
list.foldLeft(List.empty[X])(insert)
|
||||
}
|
||||
def insertSort(list: List[Int]) = list.foldLeft(List[Int]())(insert)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue