## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

arranging sorting an array Posted by chineerat on 2 Feb 2010 at 11:46 PM
Hi!
i am having trouble sorting an array using a for loop.
the problem occurs when using iterations i and i+1

for example
count:=0
for i:=0 to count do begin
if a[i] > a[i+1] then begin
....

end
count+1
end

problem is when the for loops reaches the LAST array value i , i+1 does not exist hence the problem.
I have tried using
" for i:=0 to count-1 do begin"
this does not help.

any suggestions?

Re: arranging sorting an array Posted by Atex on 3 Feb 2010 at 5:18 PM
```const array_size=10;

var t:array[1..array_size] of integer;
i,j:byte;

procedure swap(var a,b:integer);
begin
a:=a xor b;
b:=a xor b;
a:=a xor b;
end;

begin
for i:=1 to array_size do begin
end;

for i:=array_size downto 2 do  {Simple bubble sort}
for j:=1 to i do
if t[i]>t[j] then swap(t[i],t[j]);

for i:=1 to array_size do
writeln(i:2,': ',t[i]);
end.```

Re: arranging sorting an array Posted by chineerat on 4 Feb 2010 at 11:47 AM
hi!
i have listed the entire code for reference at the bottom.

however I have tried something similar to what you suggested
I am still having the trouble of the sorting 'jumping ahead'.
the problem part is
```
count :=1
begin
... values are entered

for i:=0 to count do begin

if CustomerSalary[i] > CustomerSalary[count] then begin
<arrays are sorted here>
tmpCustomerName:= CustomerName[i];
tmpCustomerLoan:= CustomerLoan[i];
tmpCustomerSalary:= CustomerSalary[i];

CustomerName[i]:= CustomerName[count];
CustomerLoan[i]:= CustomerLoan[count];
CustomerSalary[i]:= CustomerSalary[count];

CustomerName[count]:= tmpCustomerName;
CustomerLoan[count]:= tmpCustomerLoan;
CustomerSalary[count]:= tmpCustomerSalary;

end;
end;
count is incremented here

end.
```

```                Program ;

var CustomerName: array[1..25] of string;
var CustomerLoan: array[1..25] of integer;
var CustomerSalary: array[1..25] of integer;
var i,j: integer;
var tmpCustomerName:string;
var tmpCustomerLoan:integer;
var tmpCustomerSalary:integer;
var count: integer;
const array_size =25;
var YesOrNo: char;
Begin
count:=0;
FOR i:=0 TO 25 DO

begin

Writeln('ALL CUSTOMERS');
FOR i:=0 TO count DO
begin
Writeln(CustomerName[i],'----Loan:',CustomerLoan[i],'-----Salary:',CustomerSalary[i]);
end;
Writeln('---------------------------------------------------');

Writeln('Customers with Loans equal to 1');
FOR i:=0 TO count DO
begin
if CustomerLoan[i]= 1 then begin
Writeln('Loan:',CustomerLoan[i],'---Name---',CustomerName[i],'-----Salary:',CustomerSalary[i]);
end;
end;
Writeln('---------------------------------------------------');

for i:=0 to count do begin

if CustomerSalary[i] > CustomerSalary[count] then begin

tmpCustomerName:= CustomerName[i];
tmpCustomerLoan:= CustomerLoan[i];
tmpCustomerSalary:= CustomerSalary[i];

CustomerName[i]:= CustomerName[count];
CustomerLoan[i]:= CustomerLoan[count];
CustomerSalary[i]:= CustomerSalary[count];

CustomerName[count]:= tmpCustomerName;
CustomerLoan[count]:= tmpCustomerLoan;
CustomerSalary[count]:= tmpCustomerSalary;

end;
end;
end;

Writeln('5 Customers with highest salaries');
FOR i:=0 TO 5 DO
begin
Writeln('Salary:',CustomerSalary[i],'---Name----',CustomerName[i],'----Loan:',CustomerLoan[i]);
end;
Writeln('---------------------------------------------------');

count :=count +1;
end;

end.

```

Re: arranging sorting an array Posted by Atex on 4 Feb 2010 at 6:25 PM
: i have listed the entire code for reference at the bottom.
:
: however I have tried something similar to what you suggested
: I am still having the trouble of the sorting 'jumping ahead'.

Your program has way too many errors to list, it doesn't even compile. Just adapt my code to your needs...

Re: arranging sorting an array Posted by chineerat on 4 Feb 2010 at 11:50 AM
sorry for the double post
Re: arranging sorting an array Posted by quikcarl on 21 May 2010 at 8:17 PM
I use QuickSort to sort any array that needs
sorting and multiple arrays that either need
ascending or descending order. I've made my
QuickSort into a unit. Just follow what I've
done in my zip file with the example.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic