From f780fecf0e25687870afc19dcee3f22414f81e2f Mon Sep 17 00:00:00 2001 From: Anshuman7080 <164294129+Anshuman7080@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:39:01 +0000 Subject: [PATCH 1/2] optimized code --- Rock-Paper-Scissor/main.c | 117 ++++++++++---------------------------- 1 file changed, 30 insertions(+), 87 deletions(-) diff --git a/Rock-Paper-Scissor/main.c b/Rock-Paper-Scissor/main.c index 84d9e05..2f49a9e 100644 --- a/Rock-Paper-Scissor/main.c +++ b/Rock-Paper-Scissor/main.c @@ -1,97 +1,40 @@ -#include #include #include #include - -// Function to implement the game -int game(char you, char computer) -{ - // If both the user and computer - // has chose the same thing - if (you == computer) - return -1; - - // If user's choice is stone and - // computer's choice is paper - if (you == 's' && computer == 'p') - return 0; - - // If user's choice is paper and - // computer's choice is stone - else if (you == 'p' && computer == 's') return 1; - - // If user's choice is stone and - // computer's choice is scissor - if (you == 's' && computer == 'z') - return 1; - - // If user's choice is scissor and - // computer's choice is stone - else if (you == 'z' && computer == 's') - return 0; - - // If user's choice is paper and - // computer's choice is scissor - if (you == 'p' && computer == 'z') - return 0; - - // If user's choice is scissor and - // computer's choice is paper - else if (you == 'z' && computer == 'p') - return 1; + +char get_computer_choice() { + int n = rand() % 3; // Generate 0, 1, or 2 + return (n == 0) ? 's' : (n == 1) ? 'p' : 'z'; // 's' = Stone, 'p' = Paper, 'z' = Scissors } - -// Driver Code -int main() -{ - // Stores the random number - int n; - - char you, computer, result; - - // Chooses the random number - // every time - srand(time(NULL)); - - // Make the random number less - // than 100, divided it by 100 - n = rand() % 100; - - // Using simple probability 100 is - // roughly divided among stone, - // paper, and scissor - if (n < 33) - - // s is denoting Stone - computer = 's'; - - else if (n > 33 && n < 66) - - // p is denoting Paper - computer = 'p'; - - // z is denoting Scissor - else - computer = 'z'; - - printf("\n\n\n\n\t\t\t\tEnter s for STONE, p for PAPER and z for SCISSOR\n\t\t\t\t\t\t\t"); - - // input from the user - scanf("%c", &you); - - // Function Call to play the game - result = game(you, computer); - + +int determine_winner(char player, char computer) { + if (player == computer) return -1; // Draw + if ((player == 's' && computer == 'z') || (player == 'p' && computer == 's') || (player == 'z' && computer == 'p')) { + return 1; // Player wins + } + return 0; // Computer wins +} + +int main() { + srand(time(NULL)); // Seed random number generator + char player_choice, computer_choice; + + printf("\n\n\t\t\t\tEnter s for STONE, p for PAPER and z for SCISSOR: "); + scanf(" %c", &player_choice); // Space before %c to ignore leading whitespace + + computer_choice = get_computer_choice(); // Get computer choice + + int result = determine_winner(player_choice, computer_choice); // Determine the winner + if (result == -1) { printf("\n\n\t\t\t\tGame Draw!\n"); - } - else if (result == 1) { + } else if (result == 1) { printf("\n\n\t\t\t\tWow! You have won the game!\n"); - } - else { + } else { printf("\n\n\t\t\t\tOh! You have lost the game!\n"); } - printf("\t\t\t\tYOu choose : %c and Computer choose : %c\n",you, computer); - + + printf("\t\t\t\tYou chose: %c and Computer chose: %c\n", player_choice, computer_choice); + return 0; -} \ No newline at end of file +} From 1d492cb11e53755f4ed34667fa92d07945caf48b Mon Sep 17 00:00:00 2001 From: Anshuman7080 <164294129+Anshuman7080@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:57:41 +0000 Subject: [PATCH 2/2] optimized version of quick sort --- quickSort.cpp | 118 ++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 62 deletions(-) diff --git a/quickSort.cpp b/quickSort.cpp index 0d6d1bb..3a302df 100644 --- a/quickSort.cpp +++ b/quickSort.cpp @@ -1,75 +1,69 @@ -/* C++ implementation of QuickSort */ -#include +#include +#include // for std::swap +#include // for std::vector + using namespace std; -// A utility function to swap two elements -void swap(int* a, int* b) -{ - int t = *a; - *a = *b; - *b = t; +// Function to choose a pivot using the median-of-three method +int medianOfThree(int arr[], int low, int mid, int high) { + if (arr[low] > arr[mid]) + swap(arr[low], arr[mid]); + if (arr[low] > arr[high]) + swap(arr[low], arr[high]); + if (arr[mid] > arr[high]) + swap(arr[mid], arr[high]); + // Place the pivot at the end + swap(arr[mid], arr[high - 1]); + return arr[high - 1]; // Return the pivot value } -/* This function takes last element as pivot, places -the pivot element at its correct position in sorted -array, and places all smaller (smaller than pivot) -to left of pivot and all greater elements to right -of pivot */ -int partition(int arr[], int low, int high) -{ - int pivot = arr[high]; // pivot - int i - = (low - - 1); // Index of smaller element and indicates - // the right position of pivot found so far +// A utility function to partition the array +int partition(int arr[], int low, int high) { + int pivot = arr[high - 1]; // Pivot is the element just before the high index + int i = low; // Index of smaller element - for (int j = low; j <= high - 1; j++) { - // If current element is smaller than the pivot - if (arr[j] < pivot) { - i++; // increment index of smaller element - swap(&arr[i], &arr[j]); - } - } - swap(&arr[i + 1], &arr[high]); - return (i + 1); + for (int j = low; j < high - 1; j++) { + if (arr[j] < pivot) { + swap(arr[i], arr[j]); + i++; + } + } + swap(arr[i], arr[high - 1]); // Move pivot to its final place + return i; // Return the pivot index } -/* The main function that implements QuickSort -arr[] --> Array to be sorted, -low --> Starting index, -high --> Ending index */ -void quickSort(int arr[], int low, int high) -{ - if (low < high) { - /* pi is partitioning index, arr[p] is now - at right place */ - int pi = partition(arr, low, high); +// The main function that implements QuickSort +void quickSort(int arr[], int low, int high) { + while (low < high) { + // Calculate the middle index for median-of-three + int mid = low + (high - low) / 2; + int pivot = medianOfThree(arr, low, mid, high - 1); + int pi = partition(arr, low, high); - // Separately sort elements before - // partition and after partition - quickSort(arr, low, pi - 1); - quickSort(arr, pi + 1, high); - } + // Tail recursion optimization: Sort the smaller partition first + if (pi - low < high - pi) { + quickSort(arr, low, pi); + low = pi + 1; // Iterative call for the right partition + } else { + quickSort(arr, pi + 1, high); + high = pi; // Iterative call for the left partition + } + } } -/* Function to print an array */ -void printArray(int arr[], int size) -{ - int i; - for (i = 0; i < size; i++) - cout << arr[i] << " "; - cout << endl; +// Function to print an array +void printArray(const int arr[], int size) { + for (int i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; } -// Driver Code -int main() -{ - int arr[] = { 10, 7, 8, 9, 1, 5 }; - int n = sizeof(arr) / sizeof(arr[0]); - quickSort(arr, 0, n - 1); - cout << "Sorted array: \n"; - printArray(arr, n); - return 0; +// Driver code +int main() { + int arr[] = {10, 7, 8, 9, 1, 5}; + int n = sizeof(arr) / sizeof(arr[0]); + quickSort(arr, 0, n); + cout << "Sorted array: \n"; + printArray(arr, n); + return 0; } - -// This code is contributed by rathbhupendra