Recent

Author Topic: help with sorting and swapping arrays  (Read 3978 times)

thenarrator

  • New Member
  • *
  • Posts: 14
help with sorting and swapping arrays
« on: August 28, 2015, 09:32:17 am »
Hi, am trying to make a program that when prompted by the user creates, displays and sorts an array using the bubble sort method.

It fills and draws the array fine but does not carry out any sorting procedure.

Is there a problem with the way I am passing variables between the Sort and NumSwap procedures?

Thanks.
Code: [Select]
program GameMain;
uses SwinGame, sgTypes, SysUtils;
type
IntArray = array [0..24] of Integer;

procedure PopulateArray(var data: IntArray);
var
i: Integer;
begin
for i := 0 to High(data) do
begin
data[i] := Rnd(ScreenHeight());
end;
end;

procedure ShowNumbersInList(var data: IntArray);
var
i: Integer;
begin
ListClearItems('NumbersList');
for i := 0 to High(data) do
begin
ListAddItem('NumbersList', IntToStr(data[i]));
end;
end;

procedure PlotBars(var data: IntArray);
var
barWidth: Integer;
i: Integer;
x: Integer;
y: Integer;
begin
ClearScreen(ColorWhite);

barWidth := Round((ScreenWidth() - PanelWidth('NumberPanel')) / Length(data));

for i := 0 to High(data) do
begin
x := i * barWidth;
y := ScreenHeight() - data[i];
FillRectangle(ColorRed, x, y, barWidth, data[i]);
x := x + barWidth;
end;

DrawInterface();
RefreshScreen(60);
end;

procedure NumSwap(var v1: Integer; var v2: Integer);
var
temp: Integer;
begin
temp := v1;
v1 := v2;
v2 := temp;
end;

procedure Sort(var data: IntArray);
var
i, j: Integer;
begin
for i := High(data) to Low(data) do
for j := Low(data) to i - 1 do
if data[j] > data[j + 1] then
begin
NumSwap(data[j], data[j + 1]);
PlotBars(data);
Delay(100);
end
end;

procedure DoSort();
var
data: IntArray;
begin
PopulateArray(data);
ShowNumbersInList(data);
PlotBars(data);
Sort(data);
end;

procedure Main();
var
data: IntArray;
begin
    OpenGraphicsWindow('Sort Visualiser', 800, 600);
    LoadResourceBundle( 'NumberBundle.txt' );
 
    GUISetForegroundColor( ColorBlack );
    GUISetBackgroundColor( ColorWhite );
 
    ShowPanel( 'NumberPanel' );
 
    ClearScreen(ColorWhite);
 
    repeat
    ProcessEvents();
    UpdateInterface();

    DrawInterface();
    RefreshScreen(60);
 
  if ButtonClicked('SortButton') then
  begin
  DoSort();
  end;

    until WindowCloseRequested();
end;

begin
Main();
end.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: help with sorting and swapping arrays
« Reply #1 on: August 28, 2015, 12:54:30 pm »
I extracted the sorting part, it doesn't sort because the algorithm implementation is wrong. Run your sort procedure on a paper using small dataset, trace yourself where it fails.

The loop invariant for bubble sort is: at each (outermost) iteration with loop variable i, subarray [low(data) .. i] must be sorted. Does your code fulfill this invariant?

derek.john.evans

  • Guest
Re: help with sorting and swapping arrays
« Reply #2 on: August 28, 2015, 01:16:27 pm »
The loop invariant for bubble sort is: at each (outermost) iteration with loop variable i, subarray [low(data) .. i] must be sorted. Does your code fulfill this invariant?

I think Leledumbo is politely saying, read up on for/downto:

http://www.freepascal.org/docs-html/ref/refsu54.html

Code: [Select]
for i := High(data) to Low(data) do // << This doesn't look right

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: help with sorting and swapping arrays
« Reply #3 on: August 28, 2015, 01:56:40 pm »
I extracted the sorting part, it doesn't sort because the algorithm implementation is wrong. Run your sort procedure on a paper using small dataset, trace yourself where it fails.

The loop invariant for bubble sort is: at each (outermost) iteration with loop variable i, subarray [low(data) .. i] must be sorted. Does your code fulfill this invariant?

@leledumbo: It's actually -1 that (in the case of even, so odd is covered as well ;) ) , I believe but I agree.

It would also be a good idea to read something about sorting algorithms before you write code.

Get the maths first, then write code.

You will learn much more that way
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018