1. Bubble Sort (Sắp xếp nổi bọt)
Ý tưởng: Giả sử có mảng có n phần tử. Chúng ta sẽ tiến hành duyệt từ cuối lên đầu,so sánh 2 phần tử kề nhau, nếu chúng bị ngược thứ tự thì đổi vị trí, việc duyệt này bắt đầu từ cặp phần tử thứ n-1 và n. Tiếp theo là so sánh cặp phần tử thứ n-2 và n-1,… cho đến khi so sánh và đổi chỗ cặp phần tử thứ nhất và thứ hai. Sau bước này phần tử nhỏ nhất đã được nổi lên vi trí trên cùng (nó giống như hình ảnh của các “bọt” khí nhẹ hơn được nổi lên trên). Tiếp theo tiến hành với các phần tử từ thứ 2 đến thứ n.
Procedure bubblesort(var amang; Ninteger);
begin
var i,j integer;
for i=2 to N do
for j=N down to i do
if (a[j] a[j-1])
then
hoanvi(a[j-1],a[j]);
end;
begin
var i,j integer;
for i=2 to N do
for j=N down to i do
if (a[j] a[j-1])
then
hoanvi(a[j-1],a[j]);
end;
2. Selection Sort (Sắp xếp chọn)
Ý tưởng: Chọn phần tử nhỏ nhất trong n phần tử ban đầu, đưa phần tử này về vị trí đúng là đầu tiên của dãy hiện hành. Sau đó không quan tâm đến nó nữa, xem dãy hiện hành chỉ còn n-1 phần tử của dãy ban đầu, bắt đầu từ vị trí thứ 2. Lặp lại quá trình trên cho dãy hiện hành đến khi dãy hiện hành chỉ còn 1 phần tử. Dãy ban đầu có n phần tử, vậy tóm tắt ý tưởng thuật toán là thực hiện n-1 lượt việc đưa phần tử nhỏ nhất trong dãy hiện hành về vị trí đúng ở đầu dãy.
Các bước tiến hành như sau:
Bước 1: i=1
Bước 2: Tìm phần tử a[min] nhỏ nhất trong dãy hiện hành từ a[i] đến a[n]
Bước 3: Hoán vị a[min] và a[i]
Bước 4: Nếu i<=n-1 thì i=i+1; Lặp lại bước 2
Ngược lại: Dừng. n-1 phần tử đã nằm đúng vị trí.
Procedure seletionsort(var a:mang; N:byte);
var i,j: byte; min: integer;
begin
for 1:=1 to N-1 do
if (a[j] < a[min] then min:=j;
if (min <> i) then hoanvi (a[min]; a[i];
end;
Procedure hoanvi(var x,y: integer);
var tam:integer
begin
tam:=x
x:=y
y:=tam
end;
var i,j: byte; min: integer;
begin
for 1:=1 to N-1 do
if (a[j] < a[min] then min:=j;
if (min <> i) then hoanvi (a[min]; a[i];
end;
Procedure hoanvi(var x,y: integer);
var tam:integer
begin
tam:=x
x:=y
y:=tam
end;
3. Insert Sort
Procedure insertionsort(var a:mang, N:byte);
begin
var pos,i: byte; x:integer;
for i:=2 to N do
begin
x:=a[i]; pos:=i;
{sap xep tang dan}
while (pos>1 and a[pos-1]>x)do
begin
a[pos]:= a[pos-1]; dec(pos);
end;
a[pos]:= x;
end;
{sap xep giam dan}
while (pos>1)
begin
if(a[pos-1] > x)then
begin
a[pos]:= a[pos-1]; dec(pos);
end;
a[pos]:= x;
4. QuickSort
procedure Quicksort ( Var A: Mang);
Procedure Sort( Left, Right: Integer);
Var
i, j, k: Integer;
Begin
i:= Left;
j:= Right;
k:= A[(Left + Right) Div 2];
Repeat
While A[i] < k Do Inc(i);
While k < A[j] Do Dec(j);
If i <> j Then
Begin
HoanVi(A[i],A[j]);
Inc(i);
Dec(j);
end;
Until i > j;
If Left < j Then Sort(Left,j);
If i < Right Then Sort(i,Right);
end;
Begin
Sort(Left; Right);
End;
Nhãn:
Pascal mở rộng,
Pascal nâng cao
Previous Article

Code viet sai be bet. Do chu nhan
chay dc
07:29 Ngày 04 tháng 1 năm 2013
quá dởm
10:04 Ngày 18 tháng 1 năm 2013
Ai giúp bài nào hay hơn coi !!!!!!!!!!!
10:05 Ngày 18 tháng 1 năm 2013
Sắp thi Tin học trẻ rùi
10:06 Ngày 18 tháng 1 năm 2013
It's really a nice and helpful piece of info. I am happy that you just shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.
Take a look at my website ... future quotes
08:08 Ngày 24 tháng 3 năm 2013
If you want to take a great deal from this piece of writing then you have to apply such techniques to your won
website.
Look at my web blog :: commitment quotes
13:19 Ngày 26 tháng 3 năm 2013
Hi there! I could have sworn I've been to this blog before but after checking through some of the post I realized it's new to me.
Anyhow, I'm definitely delighted I found it and I'll be bookmarking and checking back frequently!
my website :: genghis khan quotes
19:42 Ngày 26 tháng 3 năm 2013
My partner and I stumbled over here different web address and thought I may as
well check things out. I like what I see so now
i am following you. Look forward to finding out about your web
page repeatedly.
Check out my web blog thanks quotes
20:54 Ngày 26 tháng 3 năm 2013
Peculiar article, totally what I needed.
Also visit my site ... future quotes
00:03 Ngày 27 tháng 3 năm 2013
When I originally commented I appear to have clicked on the -Notify me
when new comments are added- checkbox and
from now on each time a comment is added I recieve four emails with
the same comment. There has to be a means you can remove me from that service?
Thanks a lot!
Also visit my site william shakespeare quotes
01:48 Ngày 27 tháng 3 năm 2013
Excellent article. I am dealing with some of these issues as well.
.
Here is my web-site - sylvia plath quotes
02:12 Ngày 27 tháng 3 năm 2013
An outstanding share! I have just forwarded this onto a colleague
who was doing a little research on this. And he in fact bought me dinner because I
discovered it for him... lol. So let me reword this...
. Thank YOU for the meal!! But yeah, thanx for spending some time to
discuss this subject here on your web page.
Here is my website :: depressing quotes
22:39 Ngày 27 tháng 3 năm 2013
I am not sure where you're getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for wonderful info I was looking for this information for my mission.
my web blog; understanding quotes
06:55 Ngày 28 tháng 3 năm 2013
I know this if off topic but I'm looking into starting my own weblog and was curious what all is required to get setup? I'm assuming having a blog
like yours would cost a pretty penny? I'm not very internet smart so I'm not 100% sure. Any suggestions or advice would be greatly appreciated. Appreciate it
Feel free to visit my web blog - tired quotes
14:17 Ngày 02 tháng 4 năm 2013
Keep on working, great job!
Feel free to visit my web blog; frida kahlo quotes
01:38 Ngày 03 tháng 4 năm 2013
Hi! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I ended up losing months of hard
work due to no backup. Do you have any methods to protect against hackers?
Look at my web site - sylvia plath quotes
03:54 Ngày 03 tháng 4 năm 2013
code sai rất nhiều, lướt sơ qua thì bubblesort,selectionsort đã sai thuật toán,chưa tính đến lỗi cú pháp, mình hoan nghênh tinh thần chia sẻ tuy nhiên khi post bài thì chỉ nên post những gì mình thực sự hiểu hoặc nội dung mình đã kiểm chứng tính đúng đắn đối với code chương trình.
10:37 Ngày 27 tháng 4 năm 2013
Đăng nhận xét