Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with bubble sort please

uneducated_tweuneducated_twe Posts: 8Member
[b][red]This message was edited by uneducated_twe at 2004-3-6 8:57:50[/red][/b][hr]
hey folks... i need to be able to sort a file in ascending numerical order. it's a mock video rental store database, and the file stores details of the customers, so my primary key is the Customer ID.

i've chosen the bubble sort method, but it's not working, whenever the procedure starts the program just boots me to desktop. the following code includes all local variables required. other variables are self-explanatory. please help! what's wrong with my code???

procedure bubble_sort_customer;
var swap_record : customer_record_type;
recordA : customer_record_type;
recordB : customer_record_type;
loop : integer;

begin
clrscr;
reset(customer_file);

{bubble sort}
pass_no := 0;
repeat

swap_occured := False;
for loop := 1 to (counter - (pass_no - 2)) do
begin
seek(customer_file,loop);
read(customer_file,customer_record);
recordA := customer_record;
{searching & reading consecutive records}
seek(customer_file,loop+1);
read(customer_file,customer_record);
recordB := customer_record;

if (recordA.customer_ID > recordB.customer_ID) {comparing}
then begin
swap_record := recordA;
recordA := recordB; {swapping}
recordB := swap_record;

seek(customer_file,loop);
write(customer_file,recordA);
{swapping}
seek(customer_file,loop+1);
write(customer_file,recordB);

swap_occured := True;
end;
end;
pass_no := pass_no+1;
Until ((swap_occured = False) Or ((counter - (pass_no - 1)) = 1));
{bubble sort}
close(customer_file);
end;



Comments

  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by uneducated_twe at 2004-3-6 8:57:50[/red][/b][hr]
    : hey folks... i need to be able to sort a file in ascending numerical order. it's a mock video rental store database, and the file stores details of the customers, so my primary key is the Customer ID.
    :
    : i've chosen the bubble sort method, but it's not working, whenever the procedure starts the program just boots me to desktop. the following code includes all local variables required. other variables are self-explanatory. please help! what's wrong with my code???
    :
    : procedure bubble_sort_customer;
    : var swap_record : customer_record_type;
    : recordA : customer_record_type;
    : recordB : customer_record_type;
    : loop : integer;
    :
    : begin
    : clrscr;
    : reset(customer_file);
    :
    : {bubble sort}
    : pass_no := 0;
    : repeat
    :
    : swap_occured := False;
    : for loop := 1 to (counter - (pass_no - 2)) do
    : begin
    : seek(customer_file,loop);
    : read(customer_file,customer_record);
    : recordA := customer_record;
    : {searching & reading consecutive records}
    : seek(customer_file,loop+1);
    : read(customer_file,customer_record);
    : recordB := customer_record;
    :
    : if (recordA.customer_ID > recordB.customer_ID) {comparing}
    : then begin
    : swap_record := recordA;
    : recordA := recordB; {swapping}
    : recordB := swap_record;
    :
    : seek(customer_file,loop);
    : write(customer_file,recordA);
    : {swapping}
    : seek(customer_file,loop+1);
    : write(customer_file,recordB);
    :
    : swap_occured := True;
    : end;
    : end;
    : pass_no := pass_no+1;
    : Until ((swap_occured = False) Or ((counter - (pass_no - 1)) = 1));
    : {bubble sort}
    : close(customer_file);
    : end;
    :
    :
    :
    :
    It could be that you forgot to Assign() the customer_file to a filename. Or that the clrscr is wrongly supported under the OS you're running.
    I would suggest that you narrow the problem down to a line, either by using the debugger or by using several well placed writeln()'s.
  • MasterijnMasterijn Posts: 188Member
    : : [b][red]This message was edited by uneducated_twe at 2004-3-6 8:57:50[/red][/b][hr]
    : : hey folks... i need to be able to sort a file in ascending numerical order. it's a mock video rental store database, and the file stores details of the customers, so my primary key is the Customer ID.
    : :
    : : i've chosen the bubble sort method, but it's not working, whenever the procedure starts the program just boots me to desktop. the following code includes all local variables required. other variables are self-explanatory. please help! what's wrong with my code???
    : :
    : : procedure bubble_sort_customer;
    : : var swap_record : customer_record_type;
    : : recordA : customer_record_type;
    : : recordB : customer_record_type;
    : : loop : integer;
    : :
    : : begin
    : : clrscr;
    : : reset(customer_file);
    : :
    : : {bubble sort}
    : : pass_no := 0;
    : : repeat
    : :
    : : swap_occured := False;
    : : for loop := 1 to (counter - (pass_no - 2)) do
    : : begin
    : : seek(customer_file,loop);
    : : read(customer_file,customer_record);
    : : recordA := customer_record;
    : : {searching & reading consecutive records}
    : : seek(customer_file,loop+1);
    : : read(customer_file,customer_record);
    : : recordB := customer_record;
    : :
    : : if (recordA.customer_ID > recordB.customer_ID) {comparing}
    : : then begin
    : : swap_record := recordA;
    : : recordA := recordB; {swapping}
    : : recordB := swap_record;
    : :
    : : seek(customer_file,loop);
    : : write(customer_file,recordA);
    : : {swapping}
    : : seek(customer_file,loop+1);
    : : write(customer_file,recordB);
    : :
    : : swap_occured := True;
    : : end;
    : : end;
    : : pass_no := pass_no+1;
    : : Until ((swap_occured = False) Or ((counter - (pass_no - 1)) = 1));
    : : {bubble sort}
    : : close(customer_file);
    : : end;
    : :
    : :
    : :
    : :
    : It could be that you forgot to Assign() the customer_file to a filename. Or that the clrscr is wrongly supported under the OS you're running.
    : I would suggest that you narrow the problem down to a line, either by using the debugger or by using several well placed writeln()'s.
    :
    Hello,
    Maybe you can post the code of the declaration and assignment of the customer_file variable.
    I can also note that this is a very cpu and hd intensive way of sorting. You could optimize this easily by making a new record-type (TCustomerIndexRec) with the record key customer_ID and a RecordPos, like:
    [code]
    TCustomerIndexRec = record
    CustomerID : Integer; // = customer_ID
    RecordPos : Integer; // = recordpos before sorting
    end;
    [/code]
    Then scan all your data-records once while filling an array of TCustomerIndexRec records. Then you can sort this array in memory with a quicksort (or bubblesort perhaps). After this sorting you know to wich postition a record goes and you can move a record in only 1 one step.


  • uneducated_tweuneducated_twe Posts: 8Member
    [b][red]This message was edited by uneducated_twe at 2004-3-6 14:39:0[/red][/b][hr]
    hi, my file assignments are all fine, they all occur right at the start of the program, the file is assigned to c:datacustomer.dat. but thanks for the suggestion :) i know all the variable declarations work, as i'm able to add records to the file using them.

    i've narrowed it down to a line as suggested zibadian, here's another copy of the code. i've changed a few bits and pieces - mainly added a bit at the start to find and display the number of records in the file. but it still won't work... the line is marked with the error message it gives.

    i'm using Turbo Pascal 7.0 btw, in case that sheds any light on the situation.

    [code]procedure bubble_sort_customer;
    var swap_record : customer_record_type;
    recordA : customer_record_type;
    recordB : customer_record_type;

    begin
    clrscr;
    reset(customer_file);
    counter := 0;
    while not eof(customer_file) do
    begin
    read(customer_file, customer_record);
    counter := counter + 1;
    end;
    writeln(counter, ' records in the file');
    readln;
    close(customer_file);

    reset(customer_file);

    {bubble sort}
    repeat
    swap_occured := False;
    for loop := 1 to (counter - 1) do
    begin
    seek(customer_file,loop);
    read(customer_file,customer_record);
    recordA := customer_record;
    {searching & reading consecutive records}
    seek(customer_file,loop+1);
    read(customer_file,customer_record); {ERROR 100 : DISK READ ERROR}
    recordB := customer_record;

    {comparing}
    if (recordA.customer_ID > recordB.customer_ID) then
    begin
    swap_record := recordA;
    recordA := recordB; {swapping}
    recordB := swap_record;

    seek(customer_file,loop);
    write(customer_file,recordA);
    {swapping}
    seek(customer_file,loop+1);
    write(customer_file,recordB);

    swap_occured := True;
    end;
    end;
    counter := counter - 1;
    Until (swap_occured = False) or (counter = 1);
    {bubble sort}
    close(customer_file);
    end;[/code]

    as you can see, it accepts reading RecordA, but not RecordB???


  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by uneducated_twe at 2004-3-6 14:39:0[/red][/b][hr]
    : hi, my file assignments are all fine, they all occur right at the start of the program, the file is assigned to c:datacustomer.dat. but thanks for the suggestion :) i know all the variable declarations work, as i'm able to add records to the file using them.
    :
    : i've narrowed it down to a line as suggested zibadian, here's another copy of the code. i've changed a few bits and pieces - mainly added a bit at the start to find and display the number of records in the file. but it still won't work... the line is marked with the error message it gives.
    :
    : i'm using Turbo Pascal 7.0 btw, in case that sheds any light on the situation.
    :
    : [code]procedure bubble_sort_customer;
    : var swap_record : customer_record_type;
    : recordA : customer_record_type;
    : recordB : customer_record_type;
    :
    : begin
    : clrscr;
    : reset(customer_file);
    : counter := 0;
    : while not eof(customer_file) do
    : begin
    : read(customer_file, customer_record);
    : counter := counter + 1;
    : end;
    : writeln(counter, ' records in the file');
    : readln;
    : close(customer_file);
    :
    : reset(customer_file);
    :
    : {bubble sort}
    : repeat
    : swap_occured := False;
    : for loop := 1 to (counter - 1) do
    : begin
    : seek(customer_file,loop);
    : read(customer_file,customer_record);
    : recordA := customer_record;
    : {searching & reading consecutive records}
    : seek(customer_file,loop+1);
    : read(customer_file,customer_record); {ERROR 100 : DISK READ ERROR}
    : recordB := customer_record;
    :
    : {comparing}
    : if (recordA.customer_ID > recordB.customer_ID) then
    : begin
    : swap_record := recordA;
    : recordA := recordB; {swapping}
    : recordB := swap_record;
    :
    : seek(customer_file,loop);
    : write(customer_file,recordA);
    : {swapping}
    : seek(customer_file,loop+1);
    : write(customer_file,recordB);
    :
    : swap_occured := True;
    : end;
    : end;
    : counter := counter - 1;
    : Until (swap_occured = False) or (counter = 1);
    : {bubble sort}
    : close(customer_file);
    : end;[/code]
    :
    : as you can see, it accepts reading RecordA, but not RecordB???
    :
    :
    :
    It's pobably because the Seek() procedure allows you to treat the file as a 0-indexed array instead of an 1-indexed array. I assume, that the file has Counter records, which would mean that the last valid record will be Counter-1 and not Counter. I suggest that you change the for parameters into 0 and Counter-2.
  • ManningManning Posts: 1,621Member
    [b][red]This message was edited by Moderator at 2004-3-6 15:59:3[/red][/b][hr]
    : hi, my file assignments are all fine, they all occur right at the start of the program, the file is assigned to c:datacustomer.dat. but thanks for the suggestion :) i know all the variable declarations work, as i'm able to add records to the file using them.
    :
    : i've narrowed it down to a line as suggested zibadian, here's another copy of the code. i've changed a few bits and pieces - mainly added a bit at the start to find and display the number of records in the file. but it still won't work... the line is marked with the error message it gives.
    :
    : i'm using Turbo Pascal 7.0 btw, in case that sheds any light on the situation.
    :
    : [code]procedure bubble_sort_customer;
    : var swap_record : customer_record_type;
    : recordA : customer_record_type;
    : recordB : customer_record_type;
    :
    : begin
    : clrscr;
    : reset(customer_file);
    : counter := 0;
    : while not eof(customer_file) do
    : begin
    : read(customer_file, customer_record);
    : counter := counter + 1;
    : end;
    : writeln(counter, ' records in the file');
    : readln;
    : close(customer_file);
    :
    : reset(customer_file);
    :
    : {bubble sort}
    : repeat
    : swap_occured := False;
    : for loop := 1 to (counter - 1) do
    : begin
    : seek(customer_file,loop);
    : read(customer_file,customer_record);
    : recordA := customer_record;
    : {searching & reading consecutive records}
    : seek(customer_file,loop+1);
    : read(customer_file,customer_record); {ERROR 100 : DISK READ ERROR}
    : recordB := customer_record;
    :
    : {comparing}
    : if (recordA.customer_ID > recordB.customer_ID) then
    : begin
    : swap_record := recordA;
    : recordA := recordB; {swapping}
    : recordB := swap_record;
    :
    : seek(customer_file,loop);
    : write(customer_file,recordA);
    : {swapping}
    : seek(customer_file,loop+1);
    : write(customer_file,recordB);
    :
    : swap_occured := True;
    : end;
    : end;
    : counter := counter - 1;
    : Until (swap_occured = False) or (counter = 1);
    : {bubble sort}
    : close(customer_file);
    : end;[/code]
    :
    : as you can see, it accepts reading RecordA, but not RecordB???

    Lets say your file has 4 records.

    When your program runs, the counter variable will have the value 4 (there is a better way to count records using the FileSize() function, but that's not the problem).

    When your loop runs, it will run from 1 to counter - 1, or 1 to 3.

    Inside your loop, you seek to the loop index to read RecordA, and the loop index + 1 to read RecordB. That means that on the last time through the loop, you are seeking to 3 and 4. This is the problem.

    Your file has 4 records, but since things are usually 0 based (as in this case) they aren't 1 to 4, they are 0 to 3. So when you seek to position 4, you are actually seeking to the end of the file. When you try to read, you obviously can't and get an error.

    So the solution is to change your loop to also be 0 based. Try to run from 0 to counter - 2, which means on the last time through you will be seeking to position 2 and 3, which should work fine.

    [red]Edit: I see I took too long typing that, zibadian already pointed out the loop problem :-)[/red]




  • uneducated_tweuneducated_twe Posts: 8Member
    hey guys, thanks, it's worked a treat :) no more error message, i'll have to do some more extensive testings to check that it is actually sorting the file, however things are looking good so far :) and it's thankfully not something i'm kicking myself for not noticing lol, thanks!
Sign In or Register to comment.