Qbasicnews.com
February 22, 2020, 08:44:12 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: alfabetic order  (Read 6556 times)
banAnnA
Member
*
Posts: 56



« on: February 04, 2003, 08:09:03 AM »

Maybe this is a stupid question.... :Huh:
I'm asking it anyway. Why doesn't it work?
I want the words putted in printed in alfabetic order.

Code:

10 input a$(i)
if a$(i)= "stop" then goto 20
i=i+1
goto 10

20  let n=i
      let i=1
goto 30

25
let i=1
let t=t+1
let k=k+1
let i=1+t
if i=n then goto 40

30
a$(k)=a$(i)
i=i+1
if a$(i)<a$(k) then let a$(i)=a$(k)
if a$(i)=n then goto 25
goto 30

40 for c = 1 to k
print c, a$(k)
next c
Logged
QbBasta
Member
*
Posts: 71


WWW
« Reply #1 on: February 04, 2003, 04:14:08 PM »

I really don't understand your code.. But i guess you are trying to work it out by checking the ascii code.. If you have some strings and take out the first characters, make them lower or uppercase and sort the ascii values of them..

Otherwise if you are like me, lazy  Cheesy there is always the dos command sort:

sort "sourcefile" > "targetfile"

good luck
Logged

ttp://hem.passagen.se/qb.basta
Sumo Jo
Been there, done that
*****
Posts: 1244



WWW
« Reply #2 on: February 04, 2003, 07:04:40 PM »

there's that or you could do something like a bubblesort or quicksort...

Code:
Bubble Sort


While Flag$ <> "yes"
     Flag$ = "yes"
     
     For x = 1 to 'amount of words/numbers needed to be sorted minus 1

            If Ucase$(Word(x)) > Ucase$(Word(X+1)) then
                         Swap Word(x), Word(x+1)
                         Flag$ = "no"
            end if
      next x
wend



That should do any type of sorting for Words or Number arrays for you.
Logged

url=http://fileanchor.com]FileAnchor[/url] - ImageAnchor - FBTK - QbasicNews - VPlanet - Various
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #3 on: February 07, 2003, 02:06:12 AM »

But you shouldn't use bubblesort, or so they say.

Bubble sort takes exponential running time = n^2. Quicksort  = n log n. I'm SOOOOOO GOOD MAKE ME KING YESYESYES...

Anyways, there is a quicksort example in that ABC website.
Quicksort is much longer to write though.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #4 on: February 07, 2003, 03:35:52 AM »

Me and my address book....

http://relsoft.wrq.cjb.net/files/Address.bas

Check out the SortIndex sub....

Uses ShellSort...

;*)_
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
banAnnA
Member
*
Posts: 56



« Reply #5 on: February 08, 2003, 08:26:20 AM »

Thanks for the help, but it didn't really make me understand qb better. The commands you're  suggesting are far to complex (yet...) I'm really a beginner, and what I meant was really simple. I think I can get it out of a book somewhere.....some time.
Now I'm posting another question, doesn't have anything to do with this one.... ;-)
Logged
Sumo Jo
Been there, done that
*****
Posts: 1244



WWW
« Reply #6 on: February 08, 2003, 02:56:47 PM »

that is why i displayed the bubble sort.  It's one of the better sorting routines to learn first because it is so easy.  If you wanted i could explain how it works for you.  From that you can move onto something like quicksort or other sorts like that.  Wasn't there a sorting sample program that came with one of the versions of qbasic and was made by microsoft...it had like examples of a few different sorting methods and it sorted the green bars by their lengths.  Showed how fast each sorting method was and what they did.  I believe i remember seeing that somewhere before.
Logged

url=http://fileanchor.com]FileAnchor[/url] - ImageAnchor - FBTK - QbasicNews - VPlanet - Various
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #7 on: February 08, 2003, 03:37:10 PM »

ABC Archives.

Here's a bubble sort:
DIM SHARED array1%(100)

'make the array
for I% = 1 to 100
array1%(I%) = INT(RND * 500)
next I%

'now sort it
do
n% = 0
FOR I% = 1 to 99
IF array1%(I%) > array1%(I% + 1) THEN SWAP array1%(I%), array1%(I%+1): n% = 1
NEXT I%
loop until n% = 0


 :bounce:
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #8 on: February 08, 2003, 03:45:29 PM »

here's another one. not sure how it's called. Halves the bubble sort time.


FOR I% = 1 TO 100
FOR j% = I% TO 100
IF array1%(I%) > array1%(j%) THEN SWAP array1%(I%), array1%(j%)
n1% = n1% + 1
NEXT j%
NEXT I%
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #9 on: February 08, 2003, 04:30:09 PM »

It is a hack to bubblesort to achieve more speed. When I was taught bubblesort, I was taught that algorithm you posted.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
banAnnA
Member
*
Posts: 56



« Reply #10 on: February 12, 2003, 09:41:23 AM »

Quote from: "sumojo"
If you wanted i could explain how it works for you.  From that you can move onto something like quicksort or other sorts like that.

Well, that's OK. I'm interested, but you should remember that using too much prof-terms will bring even more chaos into my mind then there is now! And believe me, I am chaotic (that's also the name of a very bad music group from holland....).
I tried typing in what you suggested, but I did it in a hurry, so it's not finished, and doesn't work yet....
I shall try when having somewhat more time!
Logged
someone42
Member
*
Posts: 44


« Reply #11 on: February 16, 2003, 04:55:59 AM »

Sorting is a major area in computer science - can you believe some people in the 70s actually spent years researching more efficient ways to sort lists!

Oh well, just use a bubble sort, it's simple and probably fast enough for most purposes.
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #12 on: February 17, 2003, 07:06:49 AM »

Here's some C code of some sorting algo's:

Bubble Sort
Code:

void BubbleSort(int a[])
{
    int i,j;

    for (i=MAXLENGTH; --i >=0;) {
       swapped = 0;
       for (j=0; j<i;j++) {
          if (a[j]>a[j+1]) {
             Swap[a[j],a[j+1]);
             swapped=1;
          }
       }
       if (!swapped) return;
   }
}


Heap Sort
Code:
void Heapify(int A[],int i,int HeapSize)
{
  int left=2*i, right=2*i+1;
  int largest;

  if ((left <= HeapSize) &&
      (A[left] > A[i]))
      largest = left;
  else
      largest = i;

  if ((right <= HeapSize) &&
      (A[right] > A[largest]))
      largest = right;

  if (largest != i) {
    Swap(&A[i],&A[largest]);
    Heapify(A,largest,HeapSize);
  }
}

void HeapSort(int A[], int n)
{
     int i, HeapSize = n;

     for (i= HeapSize/2; i >= 1; i--)
         Heapify(A,i,HeapSize);

     for (i=n; i>=2; i--) {
          Swap(&A[i],&A[1]);
          HeapSize--;
          Heapify(A,1,HeapSize);
     }
}



Insertion Sort
Code:
void InsertionSort(LIST A)
{
  int f, i;
  KEYTYPE temp;

  for (f = 1; f < MAXDIM; f++) {
      if (A[f] > A[f-1]) continue;
      temp = A[f];
      i    = f-1;
      while ((i>=0)&&(A[i]>temp)) {
         A[i+1] = A[i];
         i--;
      }
      A[i+1]=temp;
  }
}



Quick Sort
Code:
int FindPivot(int A[],int l, int r)
{
    switch (choice) {
       case 1: return l;
       case 2: return pivot2(A,l,h)
       case 3: return l+random(r-l);
    }
}

int partition(int A[], int l, int r)
{
   int i,pivot, pivotpos;

   pivotpos    = FindPivot(A,l,r);
   swap(&A[l],&A[pivotpos]);
   pivotpos = l;
   pivot = A[pivotpos];

   for (i = l+1; i <= r; i++) {
     if (A[i] < pivot) {
        pivotpos++;
        swap(&A[pivotpos],&A[i]);
     }
   }

   swap(&A[l],&A[pivotpos]);
   return pivotpos;
}

void QuickSort(int A[], int l,int r,
               int threshold)
{
  int i, pivot;

  if (r-l>threshold) {
     delay(CompareDelay);
     pivot = partition(A,l,r);
 
     QuickSort(A,l,pivot-1,threshold);
     QuickSort(A,pivot+1,r,threshold);
  }
}

int pivot2(int A[], int l, int r)
{
  int i = (r+l)/2;

  if ((A[l] <= A[i]) && (A[i] <= A[r]))
     return i;
  if ((A[r] <= A[i]) && (A[i] <= A[l]))
     return i;
  if ((A[r] <= A[l]) && (A[l] <= A[i]))
     return l;
  if ((A[i] <= A[l]) && (A[l] <= A[r]))
     return l;
  if ((A[l] <= A[r]) && (A[r] <= A[i]))
     return r;
  if ((A[i] <= A[r]) && (A[r] <= A[l]))
     return r;
}


Selection Sort
Code:
void SelectSort(int A)
{
     int i, j, min, t;

     for (i =1; i<= MAXSIZE; i++) {
          min  = i;
          for (j = i+1; j<=MAXSIZE; j++)
            if (A[j] < A[min])
              min = j;
       
          Swap(&A[min],&A[i]);
     }
}


Shell Sort
Code:
void ShellSort(int A[])
{
   int i, j, h=1, v;

   do
      h = 3*h+1;
   while (h <= MAXSIZE);

   do {
      h /= 3;
      for (i=h+1; i<= MAXSIZE; i++) {
         v = A[i];
         j = i;
         while ((j>h) && (A[j-h] > v)) {
            A[j] = A[j-h];
            j -= h;
         }
         A[j] = v;
      }
   } while (h > 1);
}

Logged
ak00ma
Ancient Guru
****
Posts: 669



« Reply #13 on: February 17, 2003, 02:30:40 PM »

Hey,
at the moment we must code those sort progs in school.
But not in QB or C ... NO ... in LOGO. Do you know LOGO? It's the worst coding language I've ever seen. Coding with it is a torture for everyone.
I don't like it.
And next year we'll begin with Delphi. Hope our coding teacher will get fucked up.
Logged

B 4 EVER
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #14 on: February 17, 2003, 04:45:24 PM »

logo is nice for one or two pictures and playing with legos.

For two classes.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!