bubble sort

need an example of a bubble sort
I'm trying to sort soores from lowest to highest

Comments

Bubble Sort example ...

524gus ... Below is the TB program listing of a bubble sort example program, from Brian D Hahn's 1988 book "True BASIC by Problem Solving". It creates 100 random integers, then sorts them in ascending order. Regards ... Tom M


! BUBBLE SORT

DIM X(1000) ! NOT MORE THAN 1000 NUMBERS
RANDOMIZE
LET N=100
FOR I=1 TO N ! GENERATE RANDOM INTEGERS IN THE ..
LET X(I)=INT(N*RND)+1 ! .. RANGE 1 TO N TO TEST
NEXT I
FOR K=1 TO N-1 ! N-1 PASSES
FOR J=1 TO N-K ! N-K COMPARISONS ON EACH PASS
IF X(J)>X(J+1) THEN
LET TEMP=X(J)
LET X(J)=X(J+1)
LET X(J+1)=TEMP
END IF
NEXT J
NEXT K
FOR I=1 TO N ! PRINT THEM OUT IN ASCENDING ORDER
PRINT USING "####":X(I);
NEXT I
END

Bubble Sort Help

First of all, you have to use arrays. So if you do not how to use arrays, learn now since arrays are needed for sorting.

The code is below

LET MIN = 1 !first position of your array

LET MAX = 6 !last position of your array

LET FLAG = 0 !nothing special... just a variable as a flag

DO WHILE FLAG = 0

LET FLAG = 1

FOR X = MIN TO (MAX - 1) !the number of passes is the number !of elements - 1

IF ARRAY(X) > ARRAY(X+1) THEN !if...

LET TEMPORARY = ARRAY(X) !temporary is a variable used to hold ! the value in the certain array for right now
LET ARRAY(X) = ARRAY (X+1) !put the value of the next value in ! the array in the place of the first position because the !!!first position is greater than the x+1 position

LET ARRAY (X + 1) = TEMPORARY ! returns the value of position 1 ! to the position x + 1
LET FLAG =0 !signal to the program that another pass will be !needed to sort the array

END IF
NEXT X
LOOP

Bubble Sort

Go here:

http://maeresearch.ucsd.edu/herz/basicRef/

and look at the last screenshot. That has a TB lising for a bubble sort.

why?

why use bubble sort when you could use quick sort?