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

@ -1,9 +1,13 @@
{{Sorting Algorithm}}
In this task, the goal is to sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
The bubble sort is generally considered to be the simplest sorting algorithm. Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses. Because of its abysmal O(n<sup>2</sup>) performance, it is not used often for large (or even medium-sized) datasets.
The bubble sort is generally considered to be the simplest sorting algorithm.
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
Because of its abysmal O(n<sup>2</sup>) performance, it is not used often for large (or even medium-sized) datasets.
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them). Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass. A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
This can be expressed in pseudocode as follows (assuming 1-based indexing):
'''repeat'''

View file

@ -0,0 +1,83 @@
* Bubble Sort
BUBBLE CSECT
USING BUBBLE,R13,R12
SAVEAREA B STM-SAVEAREA(R15) skip savearea
DC 17F'0'
DC CL8'BUBBLE'
STM STM R14,R12,12(R13) save calling context
ST R13,4(R15)
ST R15,8(R13)
LR R13,R15 set addessability
LA R12,4095(R13)
LA R12,1(R12)
MORE EQU *
LA R8,0 R8=no more
LA R1,A R1=Addr(A(I))
LA R2,2(R1) R2=Addr(A(I+1))
LA R4,0 to start at 1
LA R6,1 increment
L R7,N R7=N
BCTR R7,0 R7=N-1
LOOP BXH R4,R6,ENDLOOP for R4=1 to N-1
LH R3,0(R1) R3=A(I)
CH R3,0(R2) A(I)::A(I+1)
BNH NOSWAP if A(I)<=A(I+1) then goto NOSWAP
LH R9,0(R1) R9=A(I)
LH R3,0(R2) R3=A(I+1)
STH R3,0(R1) A(I)=R3
STH R9,0(R2) A(I+1)=R9
LA R8,1 R8=more
NOSWAP EQU *
LA R1,2(R1) next A(I)
LA R2,2(R2) next A(I+1)
B LOOP
ENDLOOP EQU *
LTR R8,R8
BNZ MORE
LA R3,A R3=Addr(A(I))
LA R4,0 to start at 1
LA R6,1 increment
L R7,N
PRNT BXH R4,R6,ENDPRNT for R4=1 to N
LH R5,0(R3) R5=A(I)
CVD R4,P Store I to packed P
UNPK Z,P Z=P
MVC C,Z C=Z
OI C+L'C-1,X'F0' ZAP SIGN
MVC BUFFER(4),C+12
CVD R5,P Store A(I) to packed P
UNPK Z,P Z=P
MVC C,Z C=Z
OI C+L'C-1,X'F0' ZAP SIGN
MVC BUFFER+10(6),C+10
WTO MF=(E,WTOMSG)
LA R3,2(R3) next A(I)
B PRNT
ENDPRNT EQU *
CNOP 0,4
L R13,4(0,R13)
LM R14,R12,12(R13) restore context
XR R15,R15 set return code to 0
BR R14 return to caller
N DC A((AEND-A)/2) number of items in A, so N=F'80'
A DC H'223',H'356',H'018',H'820',H'664',H'845',H'927',H'198' 8
DC H'261',H'802',H'523',H'982',H'242',H'192',H'913',H'230' 16
DC H'353',H'565',H'195',H'174',H'665',H'807',H'050',H'539' 24
DC H'436',H'249',H'848',H'010',H'006',H'794',H'100',H'433' 32
DC H'782',H'728',H'259',H'358',H'206',H'081',H'701',H'997' 40
DC H'880',H'520',H'780',H'293',H'861',H'942',H'735',H'091' 48
DC H'503',H'582',H'716',H'836',H'135',H'653',H'856',H'142' 56
DC H'919',H'498',H'303',H'894',H'536',H'211',H'539',H'986' 64
DC H'356',H'796',H'644',H'552',H'771',H'443',H'035',H'780' 72
DC H'474',H'278',H'332',H'949',H'351',H'282',H'558',H'904' 80
AEND EQU *
P DS PL8 packed
Z DS ZL16 zoned
C DS CL16 character
WTOMSG CNOP 0,4
DC H'80' length of WTO buffer
DC H'0' must be binary zeroes
BUFFER DC 80C' '
LTORG
YREGS
END BUBBLE

View file

@ -1,42 +1,24 @@
#include <iostream>
#include <algorithm>
#include <iostream>
#include <iterator>
template< typename ARRAY_TYPE, typename INDEX_TYPE >
void bubble_sort(ARRAY_TYPE array[], INDEX_TYPE size)
{
bool done = false;
while ( ! done)
{
done = true;
for (INDEX_TYPE i = 0 ; i < size-1 ; i++)
{
if (array[i] > array[i+1])
{
done = false;
std::swap(array[i], array[i+1]);
}
}
size--;
template <typename RandomAccessIterator>
void bubble_sort(RandomAccessIterator begin, RandomAccessIterator end) {
bool swapped = true;
while (begin != end-- && swapped) {
swapped = false;
for (auto i = begin; i != end; ++i) {
if (*(i + 1) < *i) {
std::iter_swap(i, i + 1);
swapped = true;
}
}
}
}
template< typename TYPE >
void print(TYPE val)
{
std::cout << val << " ";
}
int main()
{
int array[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
bubble_sort(array, 10);
std::for_each (&array[0], &array[10], print<int>);
std::cout << std::endl;
//But in real life...
int data[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
std::sort(data, data+10);
std::for_each (data, data+10, print<int>);
std::cout << std::endl;
int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
bubble_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,16 +1,28 @@
void bubble_sort(int *a, int n) {
int j, t = 1;
while (n-- && t)
for (j = t = 0; j < n; j++) {
if (a[j] <= a[j + 1]) continue;
t = a[j], a[j] = a[j + 1], a[j + 1] = t;
t=1;
}
#include <stdio.h>
void bubble_sort (int *a, int n) {
int i, t, s = 1;
while (s) {
s = 0;
for (i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
t = a[i];
a[i] = a[i - 1];
a[i - 1] = t;
s = 1;
}
}
}
}
int main(void) {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
bubble_sort(a, sizeof(a) / sizeof(a[0]));
return 0;
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" : " ");
bubble_sort(a, n);
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
return 0;
}

View file

@ -1,145 +1,54 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. BUBBLESORT.
AUTHOR. DAVE STRATFORD.
DATE-WRITTEN. MARCH 2010.
INSTALLATION. HEXAGON SYSTEMS LIMITED.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. ICL VME.
OBJECT-COMPUTER. ICL VME.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FA-INPUT-FILE ASSIGN FL01.
SELECT FB-OUTPUT-FILE ASSIGN FL02.
DATA DIVISION.
FILE SECTION.
FD FA-INPUT-FILE.
01 FA-INPUT-REC.
03 FA-DATA PIC S9(6).
FD FB-OUTPUT-FILE.
01 FB-OUTPUT-REC PIC S9(6).
WORKING-STORAGE SECTION.
01 WA-IDENTITY.
03 WA-PROGNAME PIC X(10) VALUE "BUBBLESORT".
03 WA-VERSION PIC X(6) VALUE "000001".
01 WB-TABLE.
03 WB-ENTRY PIC 9(8) COMP SYNC OCCURS 100000
INDEXED BY WB-IX-1.
01 WC-VARS.
03 WC-SIZE PIC S9(8) COMP SYNC.
03 WC-TEMP PIC S9(8) COMP SYNC.
03 WC-END PIC S9(8) COMP SYNC.
03 WC-LAST-CHANGE PIC S9(8) COMP SYNC.
01 WF-CONDITION-FLAGS.
03 WF-EOF-FLAG PIC X.
88 END-OF-FILE VALUE "Y".
03 WF-EMPTY-FILE-FLAG PIC X.
88 EMPTY-FILE VALUE "Y".
PROCEDURE DIVISION.
A-MAIN SECTION.
A-000.
PERFORM B-INITIALISE.
IF NOT EMPTY-FILE
PERFORM C-SORT.
PERFORM D-FINISH.
A-999.
STOP RUN.
B-INITIALISE SECTION.
B-000.
DISPLAY "*** " WA-PROGNAME " VERSION "
WA-VERSION " STARTING ***".
MOVE ALL "N" TO WF-CONDITION-FLAGS.
OPEN INPUT FA-INPUT-FILE.
SET WB-IX-1 TO 0.
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG
WF-EMPTY-FILE-FLAG.
PERFORM BA-READ-INPUT UNTIL END-OF-FILE.
CLOSE FA-INPUT-FILE.
SET WC-SIZE TO WB-IX-1.
B-999.
EXIT.
BA-READ-INPUT SECTION.
BA-000.
SET WB-IX-1 UP BY 1.
MOVE FA-DATA TO WB-ENTRY(WB-IX-1).
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG.
BA-999.
EXIT.
C-SORT SECTION.
C-000.
DISPLAY "SORT STARTING".
MOVE WC-SIZE TO WC-END.
PERFORM E-BUBBLE UNTIL WC-END = 1.
DISPLAY "SORT FINISHED".
C-999.
EXIT.
D-FINISH SECTION.
D-000.
OPEN OUTPUT FB-OUTPUT-FILE.
SET WB-IX-1 TO 1.
PERFORM DA-WRITE-OUTPUT UNTIL WB-IX-1 > WC-SIZE.
CLOSE FB-OUTPUT-FILE.
DISPLAY "*** " WA-PROGNAME " FINISHED ***".
D-999.
EXIT.
DA-WRITE-OUTPUT SECTION.
DA-000.
WRITE FB-OUTPUT-REC FROM WB-ENTRY(WB-IX-1).
SET WB-IX-1 UP BY 1.
DA-999.
EXIT.
E-BUBBLE SECTION.
E-000.
MOVE 1 TO WC-LAST-CHANGE.
PERFORM F-PASS VARYING WB-IX-1 FROM 1 BY 1
UNTIL WB-IX-1 = WC-END.
MOVE WC-LAST-CHANGE TO WC-END.
E-999.
EXIT.
F-PASS SECTION.
F-000.
IF WB-ENTRY(WB-IX-1) > WB-ENTRY(WB-IX-1 + 1)
SET WC-LAST-CHANGE TO WB-IX-1
MOVE WB-ENTRY(WB-IX-1) TO WC-TEMP
MOVE WB-ENTRY(WB-IX-1 + 1) TO WB-ENTRY(WB-IX-1)
MOVE WC-TEMP TO WB-ENTRY(WB-IX-1 + 1).
F-999.
EXIT.
identification division.
program-id. BUBBLSRT.
data division.
working-storage section.
01 changed-flag pic x.
88 hasChanged value 'Y'.
88 hasNOTChanged value 'N'.
01 itemCount pic 99.
01 tempItem pic 99.
01 itemArray.
03 itemArrayCount pic 99.
03 item pic 99 occurs 99 times
indexed by itemIndex.
*
procedure division.
main.
* place the values to sort into itemArray
move 10 to itemArrayCount
move 28 to item (1)
move 44 to item (2)
move 46 to item (3)
move 24 to item (4)
move 19 to item (5)
move 2 to item (6)
move 17 to item (7)
move 11 to item (8)
move 24 to item (9)
move 4 to item (10)
* store the starting count in itemCount and perform the sort
move itemArrayCount to itemCount
perform bubble-sort
* output the results
perform varying itemIndex from 1 by 1
until itemIndex > itemArrayCount
display item (itemIndex) ';' with no advancing
end-perform
* thats it!
stop run.
*
bubble-sort.
perform with test after until hasNOTchanged
set hasNOTChanged to true
subtract 1 from itemCount
perform varying itemIndex from 1 by 1
until itemIndex > itemCount
if item (itemIndex) > item (itemIndex + 1)
move item (itemIndex) to tempItem
move item (itemIndex + 1) to item (itemIndex)
move tempItem to item (itemIndex + 1)
set hasChanged to true
end-if
end-perform
end-perform
.

View file

@ -2,7 +2,7 @@ SUBROUTINE Bubble_Sort(a)
REAL, INTENT(in out), DIMENSION(:) :: a
REAL :: temp
INTEGER :: i, j
LOGICAL :: swapped = .TRUE.
LOGICAL :: swapped
DO j = SIZE(a)-1, 1, -1
swapped = .FALSE.

View file

@ -0,0 +1,16 @@
import Data.Maybe (fromMaybe)
import Control.Monad
bubbleSortBy :: (a -> a -> Bool) -> [a] -> [a]
bubbleSortBy f as = case innerSort $ reverse as of
Nothing -> as
Just v -> let (x:xs) = reverse v
in x : bubbleSortBy f xs
where innerSort (a:b:cs) = if b `f` a
then liftM (a:) $ innerSort (b:cs)
else Just $ b : fromMaybe (a:cs)
(innerSort $ a:cs)
innerSort _ = Nothing
bsort :: Ord a => [a] -> [a]
bsort = bubbleSortBy (<)

View file

@ -0,0 +1,20 @@
function list = bubbleSort(list)
hasChanged = true;
itemCount = numel(list);
while(hasChanged)
hasChanged = false;
itemCount = itemCount - 1;
for index = (1:itemCount)
if(list(index) > list(index+1))
list([index index+1]) = list([index+1 index]); %swap
hasChanged = true;
end %if
end %for
end %while
end %bubbleSort

View file

@ -0,0 +1,15 @@
- (NSArray *) bubbleSort:(NSMutableArray *)unsorted {
BOOL done = false;
while (!done) {
done = true;
for (int i = 1; i < unsorted.count; i++) {
if ( [[unsorted objectAtIndex:i-1] integerValue] > [[unsorted objectAtIndex:i] integerValue] ) {
[unsorted exchangeObjectAtIndex:i withObjectAtIndex:i-1];
done = false;
}
}
}
return unsorted;
}

View file

@ -0,0 +1,22 @@
:- initialization(main).
bubble_sort(Xs,Res) :-
write(Xs), nl
, bubble_pass(Xs,Ys,Changed)
, ( Changed == true -> bubble_sort(Ys,Res) ; Res = Xs )
.
bubble_pass(Xs,Res,Changed) :-
Xs = [X|Ys], Ys = [Y|Zs]
, ( X > Y -> H = Y, T = [X|Zs], Changed = true
; H = X, T = Ys
)
, Res = [H|R], !, bubble_pass(T,R,Changed)
; Res = Xs
.
test([8,9,1,3,4,2,6,5,4]).
main :- test(T), bubble_sort(T,_), halt.

View file

@ -1,7 +1,7 @@
Dim sortable() As Integer ' assume the array is populated
sortable.Shuffle() ' sortable is now randomized
Dim swapped As Boolean
Do
Dim swapped As Boolean
Dim index, bound As Integer
bound = sortable.Ubound
@ -15,6 +15,5 @@
index = index + 1
Wend
If not swapped Then Exit Do
Loop
Loop Until Not swapped
'sortable is now sorted

View file

@ -1,50 +1,48 @@
/*REXX program sorts an array using the bubble-sort method. */
/*REXX program sorts an array using the bubble-sort algorithm. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call bubbleSort highItem /*invoke the bubble sort. */
call show@ ' after sort' /*show the after array elements.*/
call show@ 'before sort' /*show the before array elements.*/
call bubbleSort # /*invoke the bubble sort. */
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BUBBLESORT subroutine───────────────*/
bubbleSort: procedure expose @.; parse arg n /* n = number of items. */
bubbleSort: procedure expose @.; parse arg n /*N: number of items.*/
/*diminish # items each time. */
do until done /*sort until it's done. */
done=1 /*assume it's done (1 = true). */
do j=1 for n-1 /*sort M items this time. */
do until done /*sort until it's done. */
done=1 /*assume it's done (1 ≡ true). */
do j=1 for n-1 /*sort M items this time around. */
k=j+1 /*point to the next item. */
if @.j>@.k then do /*is it out of order ? */
_=@.j /*assign to a temp variable. */
@.j=@.k /*swap current with next. */
@.k=_ /*... and next with _ */
done=0 /*indicate it's not done. */
end /* 1=true 0=false */
if @.j>@.k then do /*is it out of order? */
_=@.j /*assign to a temp variable. */
@.j=@.k /*swap current item with next ···*/
@.k=_ /* ··· and the next with _ */
done=0 /*indicate it's not done, whereas*/
end /* [↑] 1≡true 0≡false */
end /*j*/
end /*until done*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= /*assign some default values. */
@.1 ='---letters of the Hebrew alphabet---' ; @.13='kaph [kaf]'
@.2 ='====================================' ; @.14='lamed'
@.3 ='aleph [alef]' ; @.15='mem'
@.4 ='beth [bet]' ; @.16='nun'
@.5 ='gimel' ; @.17='samekh'
@.6 ='daleth [dalet]' ; @.18='ayin'
@.7 ='he' ; @.19='pe'
@.8 ='waw [vav]' ; @.20='sadhe [tsadi]'
@.9 ='zayin' ; @.21='qoph [qof]'
@.10='heth [het]' ; @.22='resh'
@.11='teth [tet]' ; @.23='shin'
@.12='yod' ; @.24='taw [tav]'
gen@: @.= /*assign default value to all @. */
@.1 = '---letters of the Hebrew alphabet---' ; @.13 = 'kaph [kaf]'
@.2 = '====================================' ; @.14 = 'lamed'
@.3 = 'aleph [alef]' ; @.15 = 'mem'
@.4 = 'beth [bet]' ; @.16 = 'nun'
@.5 = 'gimel' ; @.17 = 'samekh'
@.6 = 'daleth [dalet]' ; @.18 = 'ayin'
@.7 = 'he' ; @.19 = 'pe'
@.8 = 'waw [vav]' ; @.20 = 'sadhe [tsadi]'
@.9 = 'zayin' ; @.21 = 'qoph [qof]'
@.10 = 'heth [het]' ; @.22 = 'resh'
@.11 = 'teth [tet]' ; @.23 = 'shin'
@.12 = 'yod' ; @.24 = 'taw [tav]'
do highItem=1 while @.highItem\=='' /*find how many entries. */
end /*highitem*/
highItem=highItem-1 /*adjust highItem slightly. */
do #=1 while @.# \=='' /*find how many entries in list. */
end /*#*/
#=#-1 /*adjust because of DO increment.*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(highItem) /*maximum width of any line. */
do j=1 for highItem
say 'element' right(j,widthH) arg(1)':' @.j
end
say copies('-',80) /*show a separator line. */
show@: widthH=length(#) /*maximum width of any line. */
do j=1 for #
say 'element' right(j,widthH) arg(1)':' @.j
end /*j*/
say copies('',80) /*show a separator line. */
return

View file

@ -0,0 +1,16 @@
function b=BubbleSort(a)
n=length(a)
swapped=%T
while swapped
swapped=%F
for i=1:1:n-1
if a(i)>a(i+1) then
temp=a(i)
a(i)=a(i+1)
a(i+1)=temp
swapped=%T
end
end
end
b=a
endfunction BubbleSort