-
First, the bubbling method sorting.
for i:=1 to n-1 do
for j:=n downto i+1 doif a[j]a[j]
then k:=j;
if i<>k
then begin t:=a[i];
a[i]:=a[k];
a[k]:=t
endend;
3. Insertion method sorting.
for i:=2 to n do
begin x:=a[i];
j:=i-1;
while (j>0) and (a[j]>x) dobegin a[j+1]:=a[j];
j:=j-1
end;a[j+1]:=x
end;Fourth, the merger method sorting.
i:=1;j:=1;
k:=1;while (i<=m) and (j<=n) dobegin if a[i]<=b[j]
then begin c[k]:=a[i];
i:=i+1
endelse begin c[k]:=b[j];
j:=j+1
end;k:=k+1
end;while i<=m do
begin c[k]:=a[i];
i:=i+1;
k:=k+1
end;while j<=n do
begin c[k]:=b[j];
j:=j+1;
k:=k+1
end;Fifth, the quick method sorting.
procedure quicksort(a,m,n);
begin i:=m;
j:=n;x:=a[m];
while i=x) and (j>i) do j:=j-1;
if j>i
then begin a[i]:=a[j];
i:=i+1;
while (a[i]<=x) and (iif ij:=j-1
endend
end;a[i]:=x;
if mif j+1
-
Bubble sort [n number (n<1000), from largest to smallest. ]:
program maopao;
var a:array[1..1000]of integer;
n,i,j,b:integer;
beginreadln(n);
for i:=1 ton do
readln(a[i]);
for i:=n downto 1 do
for j:1 to i do
if a[j]beginb:=a[j];
a[j]:=a[j+1];
a[j+1]:=b;
end;for i:=1 to n do
writeln(a[i]);
end.But this question doesn't need to bubble.
Just three variables:
program t1;
var a,b,c:integer;
beginreadln(a,b,c);
if aif celse writeln(b,a,c);
end.
-
3 variables are sufficient, and there is an algorithm that allows you to exchange the value of two variables without adding variables.
-
That's the real bubbling :
program pansy;
var a:array[1..4]of longint;i,j,t:longint;
beginfor i:=1 to 4 do read(a[i]);
for i:=1 to 3 do
for j:=1 to 4-i do
if a[j]begin
t:=a[j];
a[j]:=a[j+1];
a[j+1]:=t;
end;for i:=1 to 4 do write(a[i],' ');
end.
-
The landlord's program is an incorrect selection sort.
If you change for j:=1 to 4 do to for j:=i+1 to 4 do, you can sort from small to large.
The bubble sorter is as follows.
for i:=n-1 downto 1 dofor j:=1 to i do
if a[j]begin
temp:=a[j];
a[j]:=a[j+1];
a[j+1]:=temp
end;
-
Stacks float by.
Where is t defined?
for j:=1 to 4 do for j:=i+1 to 4 real bubbling:
for i:=1 to 4 do
for j:=1 to 4-i do
if a[j]>a[j+1] then
begint:=a[i];a[i]:=a[j];a[j]:=t;
end;Although** is a bit like it, but the meaning is completely different.
-
program ti132;
var x:array[1..100]of integer;
s,i,j,n:integer;
beginrandomize;(To activate silver, to generate random numbers, you must use) for i:= 1 to 20 do
x[i]:=trunc(random(900))+100;(random(n) produces random real numbers from 0 to n).
for s:=1 to 19 do
for j:=s+1 to 20 do
if x[s]>x[j] then beginn:=x[s];
x[s]:=x[j];
x[j]:=n;
end;for i:=1 to 20 do write(x[i],' ');
writeln;
readln;
end.
-
program xx18;
vara:array[1..20]of integer;
i,j,l:integer;
beginrandomize;
for i:=1 to 20 do
a[i]:=random(99)+1;
for i:=1 to 19 do
for j:=1 to 20-i do
if a[j]>a[j+1] then
beginl:=a[j];
a[j]:=a[j+1];
a[j+1]:=l;
end;for i:=1 to 20 do
if a[i]<>a[i+1] then write(a[i]:3);
writeln;end.
3 4 2 2 3 1 2 (third largest).
2 2 4 1 1 3 (second largest) 3
1 1 1 4 (maximum) 4 4 4
-
1 All for loops should be written like this:
for i:=1 to n-1 do
for j:=i+1 to n do
……You've written it all wrong, dizzy!! )
And the initial value of your er payable is: a number other than n;
But I'd suggest that you'd better define an x:integer instead of a[er]!
vari,j,x,n:integer;
a:array[1..1000] of integer;
beginreadln(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]begin
x:=a[i]; a[i]:=a[j]; a[j]:=x;
end;for i:=1 to n do
write(a[i],' ');
end.This program arranges the array from largest to smallest, n is how many numbers there are, if you want to enter 10 numbers, then n=10
You are a beginner, study hard, think independently, like the questions you ask in general, no one likes to waste time to answer, I am idle, I have a long character!
-
that a[er]:=a[j]; a[j]:=a[j+1];a[j+1]:=a[er] ;
It should be changed to er:=a[i]; a[i]:=a[j];a[j]:=er;
To add points o( o
-
There is a book called: National Youth Informatics Olympiad Training Textbook. The books that your school library should have, are pascal's.
-
BST is a binary lookup tree, the weight of a node, which is greater than its left son weight and less than the right son weight. In this way, if it is a balanced binary tree, then the complexity of searching for a value in the binary tree is log2(n), and n is the number of nodes.
In fact, it is very simple, it is insert, delete, and find three operations. The ideal complexity is log2(n).
It's all in the book.
BST doesn't make much sense, because if it degenerates into a chain, the complexity becomes n.
It is recommended to take a look at the balance tree, similar to splay or something.
-
varbegin...Create a linked list.
p:=head^.next;Head pointer address.
q:=head;
while p<>nil do
beginif p^.data=q^.data thenbeginq^.next:=p^.next;
dispose(p);
p:=q^.next;
end;Remove duplicate nodes (with the data domain as the object of comparison) elsebegin
q:=p;p:=p^.next;
end;If it's not the same, go down.
end;..The core you want is finished.
end.
-
Simple backtrack:
This ** was just made up by me, and it passed:
const max=9;
var m,n :longint;
a:array[1..max] of longint;
procedure work(j,k:longint);j is the j-th digit of the enumeration, and k is the last enumeration value.
var i :longint;
beginif j=m+1 then
beginfor i:=1 to m-1 do write(a[i],' ');Output.
writeln(a[m]);
endelse
for i:=k+1 to n-m+j do The range of the ith is like this, k+1 is easy to understand, and n-m+j can be mathematically proved. Example 8 3, then the first maximum range is 1 6, the second is 2 7, 3 8, and so on.
begina[j]:=i;
work(j+1,i);Backtracking.
end;end;
beginreadln(n,m);
work(1,0);
end.You should be able to understand it!!
-
This is a variant of the full permutation, which is very similar to the full permutation, and I will write it to you directly.
procedure try(d:integer);
var i,j,k:integer;
beginif d=n+1 then
beginfor i:=1 to n do
write(a[i]);
end else
beginfor i:=1 to m do
if bj[i]=false then
beginbj[i]:=true;
a[n]:=i;
try(n+1);
bj[i]:=false;
end;end;
end;read(m,n);
try(1);
This one is almost pseudo**, look at the almost.
BJ is the sentence and A is DAAN
The Pascal compilation system is a system software. Pascal is the first structured programming language with rigorous syntax, clear hierarchy, easy to write and readable programs. Pascal language is widely used in various software, and the program is divided into name (self-proposed after program), setting (defined after var), start (begin), program (body), read (read read ln), and end (end), with strong structural hierarchy, rigorous and tight. >>>More
I don't dare to be a noi master, but I have participated in noip. >>>More
Refer to the upstairs, but upstairs doesn't know then without adding a statement? >>>More