Author Topic: Sortier-Algorithmen  (Read 2948 times)

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Sortier-Algorithmen
« on: 2007-Dec-17 »
Nachtrag nach Forum-Umzug

Anm.: Seit SORTARRAY eigentlich überflüssig

Code: GLBasic [Select]
DIM a[1000]

FOR i = 0 TO 999
  a[i] = RND(1000)
NEXT

t1 = GETTIMERALL()
bubblesort(a[])
t2 = GETTIMERALL()

t3 = t2 - t1
PRINT "Zeit für Bubble Sort: " + FORMAT$(10, 2, t3) + " Millisekunden", 0 ,0
SHOWSCREEN
KEYWAIT

WHILE TRUE
  x = 0
  y = 0
  FOR i = 0 TO 999
    PRINT a[i], x, y
    INC y, 10
    IF y > 470
      y = 0
      INC x, 30
    ENDIF
  NEXT
  SHOWSCREEN
WEND


// ------------------------------------------------------------- //
// -=#  BUBBLESORT  #=-
// ------------------------------------------------------------- //
FUNCTION bubblesort: array[]

  LOCAL count = BOUNDS(array[], 0) - 1
  LOCAL notsorted = TRUE
  LOCAL i, t
 
  WHILE notsorted
    notsorted = FALSE
    FOR i = 0 TO count - 1
      IF array[i] > array[i+1]
        t = array[i]
        array[i] = array[i+1]
        array[i+1] = t
        notsorted = TRUE
      ENDIF
    NEXT
  WEND

ENDFUNCTION // BUBBLESORT
 
« Last Edit: 2010-Jan-16 by Quentin »

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Sortier-Algorithmen
« Reply #1 on: 2007-Dec-17 »
Die schnelle Sortiervariante: (abgekupfert bei einem Delphi-Listing)

Code: GLBasic [Select]
// --------------------------------- //
// Project: sort
// Start: Monday, August 20, 2007
// IDE Version: 4.192



DIM a[1000]

FOR i = 0 TO 999
  a[i] = RND(1000)
NEXT

t1 = GETTIMERALL()
quicksort(a[], 0, BOUNDS(a[], 0) - 1)
//bubblesort(a[])
t2 = GETTIMERALL()

t3 = t2 - t1
PRINT "Zeit für Quicksort: " + FORMAT$(10, 2, t3) + " Millisekunden", 0 ,0
SHOWSCREEN
KEYWAIT

WHILE TRUE
  x = 0
  y = 0
  FOR i = 0 TO 999
    PRINT a[i], x, y
    INC y, 10
    IF y > 470
      y = 0
      INC x, 30
    ENDIF
  NEXT
  SHOWSCREEN
WEND


// ------------------------------------------------------------- //
// -=#  QUICKSORT  #=-
// ------------------------------------------------------------- //
FUNCTION quicksort: array[], low, high

  LOCAL lo, hi, piv, t
 
  lo = low
  hi = high
  piv = array[high]
  WHILE TRUE
    WHILE array[lo] < piv
      INC lo, 1
    WEND
    WHILE array[hi] > piv
      DEC hi, 1
    WEND
    IF lo <= hi
      t = array[lo]
      array[lo] = array[hi]
      array[hi] = t
      INC lo, 1
      DEC hi, 1
    ENDIF
    IF lo > hi THEN BREAK  
  WEND
  IF hi > low THEN quicksort(array[], low, hi)
  IF lo < high THEN quicksort(array[], lo, high)

ENDFUNCTION // QUICKSORT
 
« Last Edit: 2010-Jan-16 by Quentin »

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5024
  • O Rly?
    • View Profile
Sortier-Algorithmen
« Reply #2 on: 2007-Dec-17 »
Irgendwie cool, auch wenn ich es atm nicht brauche!
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline WPShadow

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1667
    • View Profile
    • http://lostrevenant.blogspot.com
Sortier-Algorithmen
« Reply #3 on: 2007-Dec-18 »
Die Codes finde ich spitze! Darf ich sie (nur wenn du erlaubst) benutzen und mit meiner DB verbinden?
AMD X2 4600, 2 GB Ram, ATI X1950 XTX, XP PRO SP2: GLB Premium 10.beta_dingsi, <(´.´<) Kirby Dance (>`.`)>
http://lostrevenant.blogspot.com
alea iacta est

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Sortier-Algorithmen
« Reply #4 on: 2007-Dec-18 »
dürfen latürnich frei verwendet werden, sonst ständen sie nicht hier. Aber wie gesagt, sei SORTARRAY überflüssig
« Last Edit: 2010-Jan-16 by Quentin »