Skip to content

optimized code #381

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 30 additions & 87 deletions Rock-Paper-Scissor/main.c
Original file line number Diff line number Diff line change
@@ -1,97 +1,40 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 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;
}
}
118 changes: 56 additions & 62 deletions quickSort.cpp
Original file line number Diff line number Diff line change
@@ -1,75 +1,69 @@
/* C++ implementation of QuickSort */
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm> // for std::swap
#include <vector> // 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