***** Programmer *****
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.
***** Programmer *****

Học lập trình từ căn bản

Tìm kiếm
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» Tuyên mộ thành viên
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptyWed Oct 24, 2012 5:28 am by quangvuspkt

» Bai tap Java can ban (Phan III )
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptySat Sep 15, 2012 4:11 am by tsuyngam

» Bai tap Java can ban (Phan I )
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptySat Sep 15, 2012 4:08 am by tsuyngam

» Một số bài tập C# căn bản
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptyThu Dec 15, 2011 1:58 pm by nguyenhoduykhang

» Xem thông tin máy tính bằng c#
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptyTue Nov 08, 2011 8:19 am by namcongtu288

» My First Browser Tree Program in VB.NET
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptyFri Aug 12, 2011 5:50 pm by kimthaohg85

» Giúp em bài C++ này với
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptySun Jul 31, 2011 12:19 pm by kubin

» GIUP DO XAY DUNG BO GO TIENG VIET CODE C#
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptySun Jul 24, 2011 8:40 pm by phonui82

» CHÀO TẤT CẢ CÁC THÀNH VIÊN TRONG DIỄN ĐÀN
QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C EmptyTue Jul 19, 2011 9:28 am by phonui82

Đăng Nhập

Quên mật khẩu



April 2024
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
2930     

Calendar Calendar


You are not connected. Please login or register

QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C

Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

Tesulakata

Tesulakata
Thành viên
Thành viên

QUICK SOFT- ẢNH MINH HỌA- SẮP XẾP NHANH- 80 DÒNG LỆNH C
http://cprogramminglanguage.net/quicksort-algorithm-c-source-code.aspx


Quicksort sorts a list based on the divide and conquer strategy. In quicksort algorithm we divide the list into two sub-lists, sort these sub-lists and recursively until the list is sorted; The basic steps of quicksort algorithm are as follows:

1. Choose a key element in the list which is called a pivot.
2. Reorder the list with the rule that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it. After the partitioning, the pivot is in its final position.
3. Recursively reorder two sub-lists: the sub-list of lesser elements and the sub-list of greater elements.

Here is the animation to visualize how quicksort algorithm works

Here is source code which is implemented in C programming language to demonstrate how quicksort algorithm works:
view source
print?
01 #include <stdio.h>
02 #include <stdlib.h>
03
04 void swap(int *x,int *y)
05 {
06 int temp;
07 temp = *x;
08 *x = *y;
09 *y = temp;
10 }
11
12 int choose_pivot(int i,int j )
13 {
14 return((i+j) /2);
15 }
16
17 void quicksort(int list[],int m,int n)
18 {
19 int key,i,j,k;
20 if( m < n)
21 {
22 k = choose_pivot(m,n);
23 swap(&list[m],&list[k]);
24 key = list[m];
25 i = m+1;
26 j = n;
27 while(i <= j)
28 {
29 while((i <= n) && (list[i] <= key))
30 i++;
31 while((j >= m) && (list[j] > key))
32 j--;
33 if( i < j)
34 swap(&list[i],&list[j]);
35 }
36 // swap two elements
37 swap(&list[m],&list[j]);
38 // recursively sort the lesser list
39 quicksort(list,m,j-1);
40 quicksort(list,j+1,n);
41 }
42 }
43 void printlist(int list[],int n)
44 {
45 int i;
46 for(i=0;i<n;i++)
47 printf("%d\t",list[i]);
48 }
49
50 void main()
51 {
52 const int MAX_ELEMENTS = 10;
53 int list[MAX_ELEMENTS];
54
55 int i = 0;
56
57 // generate random numbers and fill them to the list
58 for(i = 0; i < MAX_ELEMENTS; i++ ){
59 list[i] = rand();
60 }
61 printf("The list before sorting is:\n");
62 printlist(list,MAX_ELEMENTS);
63
64 // sort the list using quicksort
65 quicksort(list,0,MAX_ELEMENTS-1);
66
67 // print the result
68 printf("The list after sorting using quicksort algorithm:\n");
69 printlist(list,MAX_ELEMENTS);
70 }

https://programmer.4umer.com

Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết