Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -2,6 +2,7 @@
In this task, the goal is to sort an [[array]] (or list) of elements using the Selection sort algorithm. It works as follows:
First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.
Its asymptotic complexity is [[O]](n<sup>2</sup>) making it inefficient on large arrays. Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg writing to flash memory or EEPROM. No other sorting algorithm has less data movement.
Its asymptotic complexity is [[O]](n<sup>2</sup>) making it inefficient on large arrays. Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM.
No other sorting algorithm has less data movement.
For more information see the article on [[wp:Selection_sort|Wikipedia]].

View file

@ -1,8 +1,17 @@
#include <algorithm>
#include <iterator>
#include <iostream>
template<typename ForwardIterator>
void selectionSort(ForwardIterator begin, ForwardIterator end) {
for(ForwardIterator i = begin; i != end; ++i)
std::iter_swap(i, std::min_element(i, end));
template<typename ForwardIterator> void selection_sort(ForwardIterator begin,
ForwardIterator end) {
for(auto i = begin; i != end; ++i) {
std::iter_swap(i, std::min_element(i, end));
}
}
int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
selection_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

View file

@ -1,9 +1,12 @@
#include <stdio.h>
void selection_sort (int *a, int n) {
int i, j, m, t;
for (i = 0; i < n; i++) {
for (j = i, m = i; j < n; j++) {
if (a[j] < a[m])
if (a[j] < a[m]) {
m = j;
}
}
t = a[i];
a[i] = a[m];
@ -14,6 +17,11 @@ void selection_sort (int *a, int n) {
int main () {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
int i;
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
selection_sort(a, n);
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
return 0;
}

View file

@ -0,0 +1,62 @@
class
SELECTION_SORT[G -> COMPARABLE]
feature {NONE}
index_of_min(ar: ARRAY [G]; lower: INTEGER):INTEGER
--find index of smallest element in array ar in the range of lower and the max index.
require
lower_positiv : lower >=1
lower_in_range: lower <= ar.count
ar_not_void: ar/= Void
local
i, index: INTEGER
min: G
do
from
i:=lower
min := ar.item (i)
index := i
until
i+1 > ar.count
loop
if ar.item(i+1) < min then
min := ar.item(i+1)
index := i+1
end
i := i + 1
end
Result := index
ensure
result_is_set: Result /= Void
end
sort (ar: ARRAY [G]):ARRAY[G]
-- sort array ar with selectionsort
require
ar_not_void: ar/=VOID
local
min_index: INTEGER
ith: G
do
across ar as ic loop
min_index := index_of_min(ar,ic.cursor_index)
ith:=ar[ic.cursor_index]
ar[ic.cursor_index]:= ar[min_index]
ar[min_index]:=ith
end
Result:= ar
ensure
Result_is_set: Result /= Void
end
feature
selectionsort(ar: ARRAY[G]):ARRAY[G]
do
Result:= sort(ar)
end
end

View file

@ -0,0 +1,23 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
test := <<1, 27, 32, 99, 1, -7, 3, 5, 7>>
io.put_string ("Unsorted: ")
across test as ic loop io.put_string (ic.item.out + " ") end
create selection
io.put_string ("%NSorted: ")
test:= selectionsort.selectionsort(test)
across test as ar loop io.put_string (ar.item.out + " ") end
end
test: ARRAY[INTEGER]
selection: SELECTION_SORT[INTEGER]
end

View file

@ -1,7 +1,7 @@
import Data.List (unfoldr)
selectionSort = unfoldr selectionSort' where
selectionSort' [] = Nothing
selectionSort' (first:lst) = Just $ foldl f (first, []) lst
f (small, output) x | x < small = (x, small:output)
| otherwise = (small, x:output)
selSort :: (Ord a) => [a] -> [a]
selSort [] = []
selSort xs = let x = maximum xs in selSort (remove x xs) ++ [x]
where remove _ [] = []
remove a (x:xs)
| x == a = xs
| otherwise = x : remove a xs

View file

@ -1,31 +1,31 @@
/*REXX program sorts an array using the selection-sort method. */
@.='' /*assign a default value to stem.*/
@.1='---The seven hills of Rome:---'
@.2='=============================='
@.3='Caelian'
@.4='Palatine'
@.5='Capitoline'
@.6='Virminal'
@.7='Esquiline'
@.8='Quirinal'
@.9='Aventine'
do items=1 until @.items=='' /*find # items.*/
end
items=items-1 /*adjust the # of items slightly.*/
call show@ 'before sort' /*show the before array elements,*/
call selectionSort items /*invoke the selection sort. */
call show@ ' after sort' /*show the after array elements.*/
/*REXX program sorts a stemmed array using the selection-sort algorithm.*/
@. = /*assign a default value to stem.*/
@.1 = '---The seven hills of Rome:---'
@.2 = '=============================='
@.3 = 'Caelian'
@.4 = 'Palatine'
@.5 = 'Capitoline'
@.6 = 'Virminal'
@.7 = 'Esquiline'
@.8 = 'Quirinal'
@.9 = 'Aventine'
do k=1 until @.k=='' /*find the number of array items.*/
end /*k*/ /* [↑] find the "null" item. */
items=k-1 /*adjust the # of items slightly.*/
call show@ 'before sort' /*show the before array elements,*/
call selectionSort items /*invoke the selection sort. */
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SELECTIONSORT subroutine────────────*/
selectionSort: procedure expose @.; parse arg n
do j=1 for n-1
_=@.j; p=j; do k=j+1 to n
if @.k<_ then do; _=@.k; p=k; end
end /*k*/
if p==j then iterate
_=@.j; @.j=@.p; @.p=_
end /*j*/
do j=1 for n-1
_=@.j; p=j; do k=j+1 to n
if @.k<_ then do; _=@.k; p=k; end
end /*k*/
if p==j then iterate /*if the same, order of items OK.*/
_=@.j; @.j=@.p; @.p=_ /*swap two items out-of-sequence.*/
end /*j*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: w=length(items); do i=1 for items