nguoimoi91 Tài Sản của : nguoimoi91 Tiêu đề: Hàm Secletion Sort Sat Feb 26, 2011 2:32 pmღ...............(¯™º•º†•»-»(¯`† Lớp 09CT112 Forum †´¯)«-«•†º•™¯)...............ღ #include <iostream> #include <stdio.h> #include <conio.h> #include <time.h> using namespace std; void Generate (int A[], int n) //hàm phát sinh ngẫu nhiên các phần tử cho mạng { srand(time(NULL)); //khởi tạo bộ phát sinh ngẫu nhiên theo nhịp đồng hồ for (int i = 0; i < n; i++) A[i] = rand() % 1000 +1; //phát sinh giá trị ngẫu nhiên cho mảng } void Hoanvi (int &a, int &b) { int c = a; a = b; b = c; } void SelectionSort(int A[], int n, int &ss, int &gan, double &tg) { ss = gan =0; clock_t bd = clock(); //đo số xung nhịp khi bắt đẩu thực hiện for (int i =0; i< n -i; i++) { int min =i; for (int j=i+1; j < n; j++) { ss++; //đếm số phép so sánh if (A[j] < A[min]) min = j; } if (min != i ) { gan += 3; //đếm số phép gán Hoanvi(A[i], A[min]); } } clock_t kt = clock(); //đo số xung nhịp khi kết thúc tg = (double) (kt -bd)/CLOCKS_PER_SEC; //tính thời gian thực hiện } void interchange(int A[], int n, int &ss, int &gan, double &tg) { ss = gan = 0; clock_t bd = clock(); //đo số xung nhịp khi bắt đầu for (int i = 0; i < n -1; i++) for (int j = i + 1; j < n; j++) { ss++; if (A[i] > A[j]) { gan +=3 ; Hoanvi(A[i], A[j]); } } } void main () { int A[10000], n =10000, ss, gan; double tg; Generate (A, n); SelectionSort(A, n, ss, gan, tg); cout<<"Selection Sort:"<<endl; cout<<" -thoi gian thuc hien: "<<tg<<endl; cout<<" -so phep so sanh: "<<ss<<endl; cout<<" -so phep gan: "<<gan<<endl; getch(); }
nguoimoi91 Tài Sản của : nguoimoi91 Tiêu đề: Re: Hàm Secletion Sort Sat Feb 26, 2011 3:18 pmღ...............(¯™º•º†•»-»(¯`† Lớp 09CT112 Forum †´¯)«-«•†º•™¯)...............ღ
Cùi Mía