Advertisement
skb50bd

CSE245_LAB01_SUBMISSION

Sep 23rd, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #define SIZE 1000000
  7. #define RANGE 10000
  8. using namespace std;
  9.  
  10. int A[SIZE], B[SIZE];
  11.  
  12. void swap (long long &X, long long &Y) {
  13.     int temp = X;
  14.     X = Y;
  15.     Y = temp;
  16. }
  17.  
  18. void insertionSort (int *A, long long N) {
  19.     for (int i = 1; i < N; i++)
  20.         for (int j = i - 1; j >= 0 && A[j] > A[j + 1]; j--)
  21.             swap (A[j], A[j + 1]);
  22. }
  23.  
  24. void bubbleSort (int *A, long long N) {
  25.     for (long long i = 0; i < N - 1; i++)
  26.         for (long long j = N - 1; j >= i + 1; j--)
  27.             if (A[j] < A[j - 1])
  28.                 swap (A[j], A[j - 1]);
  29. }
  30.  
  31. void selectionSort (int *A, long long N) {
  32.     for (int i = 0, k; i < N - 1; i++) {
  33.         k = i;
  34.         for (int j = i + 1; j < N; j++)
  35.             if (A[k] > A[j])
  36.                 k = j;
  37.         if (i != k)
  38.             swap (A[i], A[k]);
  39.     }
  40. }
  41.  
  42. int sequentialSearch (int *A, int N, int x) {
  43.     for (int i = 0; i < N; i++) {
  44.         if (A[i] == x)
  45.             return i;
  46.     }
  47.     return -1;
  48. }
  49.  
  50. int binarySearch (int *A, int lo, int hi, int x) {
  51.     if (lo == hi) {
  52.         if (A[lo] == x)
  53.             return lo;
  54.         else
  55.             return -1;
  56.     }
  57.     int mid = (lo + hi) / 2;
  58.     if (A[mid] >= x)
  59.         return binarySearch (A, lo, mid, x);
  60.     else
  61.         return binarySearch (A, mid + 1, hi, x);
  62. }
  63.  
  64. int binarySearchIterative (int *A, int N, int x) {
  65.     int lo = 0;
  66.     int hi = N - 1;
  67.     int mid;
  68.  
  69.     while (lo <= hi) {
  70.         mid = (lo + hi) / 2;
  71.         if (A[mid] > x)
  72.             hi = mid - 1;
  73.         else if (A[mid] < x)
  74.             lo = mid + 1;
  75.         else
  76.             return mid;
  77.     }
  78.     return -1;
  79. }
  80.  
  81. void display (int *A, long long N) {
  82.     for (int i = 0; i < N; i++)
  83.         cout << A[i] << " ";
  84.     cout << endl << endl;
  85. }
  86.  
  87.  
  88. int main() {
  89.     ofstream outfile;
  90.     outfile.open("random.txt");
  91.     srand(time(NULL));
  92.     for (int i = 0; i < SIZE; i++)
  93.         outfile << rand() % RANGE << " ";
  94.     outfile << endl;
  95.     outfile.close();
  96.  
  97.     ifstream infile;
  98.     infile.open("random.txt");
  99.  
  100.     cout << "Reading " << SIZE << " Inputs from the File..." << endl;
  101.     for (long long i = 0; i < SIZE; i++)
  102.         infile >> B[i];
  103.     infile.close();
  104.  
  105.     long long Start, End;
  106.  
  107.     cout << "INSERTION SORT" << endl;
  108.     cout << "--------------" << endl;
  109.     for (long long i = 0; i < SIZE; i++)
  110.         A[i] = B[i];
  111. //    cout << "Unsorted Array: ";
  112. //    display(A, SIZE);
  113. //    cout << "Sorting..." << endl;
  114.     Start = clock();
  115.     insertionSort (A, SIZE);
  116.     End = clock();
  117. //    cout << "Sorted Array: ";
  118. //    display (A, SIZE);
  119.     cout << "Insertion Sort took " << fixed << setprecision(4) << (End - Start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  120.  
  121.  
  122.     cout << "BUBBLE SORT" << endl;
  123.     cout << "-----------" << endl;
  124.     for (long long i = 0; i < SIZE; i++)
  125.         A[i] = B[i];
  126. //    cout << "Unsorted Array: ";
  127. //    display(A, SIZE);
  128. //    cout << "Sorting..." << endl;
  129.     Start = clock();
  130.     bubbleSort (A, SIZE);
  131.     End = clock();
  132. //    cout << "Sorted Array: ";
  133. //    display (A, SIZE);
  134.     cout << "Bubble Sort took " << fixed << setprecision(4) << (End - Start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  135.  
  136.  
  137.     cout << "SELECTION SORT" << endl;
  138.     cout << "--------------" << endl;
  139.     for (long long i = 0; i < SIZE; i++)
  140.         A[i] = B[i];
  141. //    cout << "Unsorted Array: ";
  142. //    display(A, SIZE);
  143. //    cout << "Sorting..." << endl;
  144.     Start = clock();
  145.     selectionSort (A, SIZE);
  146.     End = clock();
  147. //    cout << "Sorted Array: ";
  148. //    display (A, SIZE);
  149.     cout << "Selection Sort took " << fixed << setprecision(4) << (End - Start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  150.  
  151.  
  152.  
  153.     cout << "Enter the Value to Search: ";
  154.     int x;
  155.     cin >> x;
  156.  
  157.  
  158.     cout << "SEQUENTIAL SEARCH" << endl;
  159.     cout << "-----------------" << endl;
  160.     cout << "Searching " << x << " in the Array...." << endl;
  161.     Start = clock();
  162.     int pos = sequentialSearch(A, SIZE, x);
  163.     End = clock();
  164.     if (pos >= 0)
  165.         cout << x << " Found on Position: " << pos << endl;
  166.     else
  167.         cout << x << " Not Found" << endl;
  168.     cout << "Sequential Search took " << fixed << setprecision(4) << (End - Start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  169.  
  170.  
  171.     cout << "BINARY SEARCH" << endl;
  172.     cout << "-----------------" << endl;
  173.     cout << "Searching " << x << " in the Array...." << endl;
  174.     Start = clock();
  175.     End = clock();
  176.     pos = binarySearch (A, 0, SIZE - 1, x);
  177.     if (pos >= 0)
  178.         cout << x << " Found on Position: " << pos << endl;
  179.     else
  180.         cout << x << " Not Found" << endl;
  181.     cout << "Binary Search took " << fixed << setprecision(4) << (End - Start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  182.  
  183.  
  184.     cout << "BINARY (Iterative) SEARCH" << endl;
  185.     cout << "-----------------" << endl;
  186.     cout << "Searching " << x << " in the Array...." << endl;
  187.     Start = clock();
  188.     End = clock();
  189.     pos = binarySearchIterative (A, SIZE, x);
  190.     if (pos >= 0)
  191.         cout << x << " Found on Position: " << pos << endl;
  192.     else
  193.         cout << x << " Not Found" << endl;
  194.     cout << "Binary Search (Iteratieve) took " << fixed << setprecision(4) << (End - Start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  195.  
  196.  
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement