World Of Warcraft, WoW Pointer 14

Senin, 30 Maret 2015

Perbandingan Kecepatan Eksekusi Bubble Sort, Selection Sort, Dan Insertion Sort



Kali ini saya akan memposting tentang koding menghitung perbandingan kecepatan eksekusi tiap-tiap algoritma (bubble sort, selection sort, dan insertion sort).

Langsung saja kita koding.

#include <iostream>
#include <windows.h>

using namespace std;

#define SIZE 8

void bubsort(void);
void selsort(void);
void inssort(void);

int n[SIZE];
double get_time(){
          LARGE_INTEGER t, f;
          QueryPerformanceCounter(&t);
          QueryPerformanceFrequency(&f);
          return (double)t.QuadPart/(double)f.QuadPart;
          }
int main(void) {
          int i;
          double a, b, c;
          cout<<"data acak:"<<endl;

          for(i = 0; i < SIZE; i++) {
                      cout<<"data ke "<< i<<": ";
                      cin>>n[i];
          }
          cout<<endl;
          cout<<"Hasil Perbandingan"<<endl;
         
          bubsort();
          a=get_time();
          cout<<endl<<"Bubble Sort     :";
          for(i = 0; i < SIZE; i++)
                      cout<<n[i];      
          b=get_time();
          c=b-a;
          cout <<endl<<"Lama Pengurutan      : " << c << " detik" << endl;
         
          selsort();
          a=get_time();
          cout<<endl<<"Selection Sort :";
          for(i = 0; i < SIZE; i++)
                      cout<< n[i];
          b=get_time();
          c=b-a;
          cout <<endl<<"Lama Pengurutan      : " << c << " detik" << endl;
         
          inssort();
          a=get_time();
          cout<<endl<<"Insertion Sort   :";
          for(i = 0; i < SIZE; i++)
                      cout<< n[i];
          b=get_time();
          c=b-a;
          cout <<endl<<"Lama Pengurutan      : " << c << " detik" << endl;
}

void inssort(void) {
          int i, s, m;
         
          for(i = 1; i < SIZE; i++) {                                            
                      m = n[i];
                      s = i;

                      while(s >= 0 && m < n[s - 1]) {
                                  n[s] = n[s-1];
                                  s = s-1;
                      }
                     
                      n[s] = m;
          }
}

void selsort(void) {
          int i, j, min, tmp;
         
          for(i = 0; i < SIZE; i++) {                                            
                      min = i;
                      for(j = i; j < SIZE; j++)
                                  if(n[j] < n[min])
                                              min = j;

                      tmp = n[i];
                      n[i] = n[min];
                      n[min] = tmp;
          }
}

void bubsort(void) {
          int i, j, tmp;
          for(i = 0; i < SIZE - 1; i++) {                                       
                      for(j = 0; j < SIZE - i - 1; j++) {
                                  if(n[j] > n[j + 1]) {
                                              tmp = n[j];
                                              n[j] = n[j + 1];
                                              n[j + 1] = tmp;
                                  }
                      }
          }
}


Dan ini screensort dari koding di atas.



Tidak ada komentar:

Posting Komentar