Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
alfabetic order
#11
Quote: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!
Reply
#12
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.
Reply
#13
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[align=left] > A[i]))
      largest = left;
  else
      largest = i;

  if ((right <= HeapSize) &&
      (A[align=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);
}
Reply
#14
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.
B 4 EVER
Reply
#15
logo is nice for one or two pictures and playing with legos.

For two classes.
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.
Reply
#16
Quote:Hey,
at the moment we must code those sort progs in school.
But not in QB or C ... NO ... in LOGO

I feel sorry for you.

Have you considered asking the teacher what the whole point of this is? You're probably going to spend more time writing hacks to get logo to do stuff than figuring out how sorting works. Sad
Reply
#17
Well, I finally did it. I feel kind of stupid right now, was making the same mistake all the time......
But this is clear to me now.

Quote: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%

the c code is far to complicated for me, so I wont try any of the examples given. I'll leave that with you (you refers to all experienced programmers and -wannabees....). Apart from that, it doesn't work in qb, so why should I try? Anyway, thanks for the tips.
Reply
#18
here's a truncated quicksort that I stole (and truncated, greatly) from a site. It uses recursion, though. That is not as efficient as iteration with arrays. Perhaps someone could make it iterative with arrays. Sad

Code:
DECLARE SUB DoSort (SortArray() AS INTEGER, Low AS INTEGER, High AS INTEGER)
array.max% = 250
DIM array1(array.max%) AS INTEGER
DIM number AS INTEGER
'generate a random array.
RANDOMIZE TIMER
CLS : FOR I% = 1 TO array.max%: array1(I%) = INT(RND * 5000) + 1: NEXT I%
DoSort array1(), 1, array.max%
'print the sorted array.
CLS : PRINT "Final sorted array :": PRINT
FOR I% = 1 TO array.max%: PRINT array1(I%); : NEXT I%

SUB DoSort (SortArray() AS INTEGER, Low AS INTEGER, High AS INTEGER)
DIM Lower AS INTEGER: DIM Higher AS INTEGER
DIM RandIndex AS INTEGER: DIM Partition AS INTEGER
IF Low < High THEN
IF High - Low = 1 THEN
IF SortArray(Low) > SortArray(High) THEN
SWAP SortArray(Low), SortArray(High)
END IF
ELSE
RandIndex = INT(RND * (High - Low + 1)) + Low
SWAP SortArray(High), SortArray(RandIndex)
Partition = SortArray(High)
DO
Lower = Low
Higher = High
DO WHILE (Lower < Higher) AND (SortArray(Lower) <= Partition)
Lower = Lower + 1
LOOP
DO WHILE (Higher > Lower) AND (SortArray(Higher) >= Partition)
Higher = Higher - 1
LOOP
IF Lower < Higher THEN SWAP SortArray(Lower), SortArray(Higher)
LOOP WHILE Lower < Higher
SWAP SortArray(Lower), SortArray(High)
IF (Lower - Low) < (High - Lower) THEN
DoSort SortArray(), Low, Lower - 1: DoSort SortArray(), Lower + 1, High
ELSE
DoSort SortArray(), Lower + 1, High: DoSort SortArray(), Low, Lower - 1
END IF
END IF
END IF
END SUB
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.
Reply
#19
'Here I have the words loaded as a DATA script, but it's easily 'modifiable if you wanted to be prompted for the words.

REM Bubble sort
REM Create flags
SCREEN 12
CONST True% = 1
CONST False% = 0

DIM friends$(15)
DATA mary, tom, dick, adam, zeke, paul, stanley, stuart, maude, maryjane, marie, maryellen, maria, matilda, marjorie
FOR load.subscript = 1 TO 15 STEP 1
READ friends$(load.subscript)
NEXT load.subscript

CLS
COLOR 1
PRINT "Out of order"
LINE (0, 14)-(95, 14), 2
COLOR 15
FOR i = 1 TO 15
PRINT friends$(i)
NEXT i
SLEEP
DO
OutOfOrder = False%
FOR i = 1 TO 15 - 1
IF UCASE$(friends$(i)) > UCASE$(friends$(i + 1)) THEN
SWAP friends$(i), friends$(i + 1)
OutOfOrder = True%
END IF
NEXT i
PRINT
LOOP WHILE OutOfOrder
LOCATE 1, 20
COLOR 1
PRINT "Alphabetized"
LINE (150, 14)-(246, 14), 2
COLOR 15
FOR i = 1 TO 15
LOCATE (i + 1), 20
PRINT friends$(i)
NEXT i
END
n a world as crazy as this one, it ought to be easy to find something that happens solely by chance. It isn't.

Kevin McKeen
The Orderly Pursuit of Pure Disorder.
Discover, January, 1981
Reply
#20
Quote: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.

Thanks man, that method really helped me out (although I find it works better without UCASE$())
earn.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)