Skip to content
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
51 changes: 51 additions & 0 deletions Bogo_sort/permutation_bogosort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// C++ implementation of Bogo Sort
#include<bits/stdc++.h>
using namespace std;
static bool IsSorted(int* data, int count)
{
while (--count >= 1)
if (data[count] < data[count - 1]) return false;

return true;
}

static void Shuffle(int* data, int count)
{
int temp, rnd;

for (int i = 0; i < count; ++i)
{
rnd = rand() % count;
temp = data[i];
data[i] = data[rnd];
data[rnd] = temp;
}
}

static void BogoSort(int* data, int count)
{
while (!IsSorted(data, count))
Shuffle(data, count);
}

// prints the array
void printArray(int a[], int n)
{
for (int i=0; i<n; i++)
printf("%d ", a[i]);
printf("\n");
}

int main()
{
int n;
cout<<"enter size of array"<<endl;
cin>>n;
int* a = new int[n];
cout<<"elements of array"<<endl;
for(int i=0;i<n;i++)cin>>a[i];
BogoSort(a, n);
printf("Sorted array :\n");
printArray(a,n);
return 0;
}
4 changes: 4 additions & 0 deletions Bogo_sort/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// C++ implementation of Bogo Sort
//best time complexity is order of Big O(n)
//avg time complexity is order of Big O(n*n!) or O((n+1)!)
//worst case is infinity
40 changes: 40 additions & 0 deletions GettingStarted/bubblesort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}


void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}


int main()
{
int arr[] = {45, 11, 99, 67, 27, 19, 2};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}

7 changes: 7 additions & 0 deletions GettingStarted/helloworld.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include<iostream>
using namespace std;
int main{

cout<<"\nHello World\n";
return 0;
}
102 changes: 102 additions & 0 deletions GettingStarted/maxTwoNumbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// continuing the journey to learn C++
// just using two numbers !! ;)
// Now, we want to find the maximum of two numbers.

#include <iostream>
using namespace std;

int main(){

int num1, num2;
cout << "Please enter two numbers:\n"; // prompt user to enter numbers
cin >> num1 >> num2;

/*
So, now, let us learn about CONDITIONAL STATEMENTS and RELATIONAL OPERATORS
How do you check which number is larger among two numbers?
Well, that is common sense. You can simply tell by seeing the numbers.
But, computer is a dumb machine, it needs to be told everything step by step.

There is a logical operator greater than (>) similar to addition operator (+),
like the addition operator returns sum of numbers, the greater than operator...
when applied in the fashion (a > b)...
...It seems you are thinking right! It returns 0 or 1, 0 when a is not greater
than b and 1 when a is actually greater than b.

So, any relational operator(>, <, <=, >=, ==, !=) returns a boolean value,
- 0 when the relation doesn't hold
- 1 when the relation holds.

NOTE(*):
- To check whether 2 numbers are equal, we use == (two equal signs without space)
since we already have used = (single equal sign) as assignment operator.
- We also have operator to check whether 2 numbers are not equal,
"!=" is this magical operator.

Now, that we know how to tell computer to compare two numbers, what next?
It will just tell whether num1 > num2 or not! But, we want to print the max no!
So, we introduce you the magical and the powerful: conditional statements,
the most important one is "if then else".
Well, if num1 > num2 then num1 will be the max
otherwise num2 would be the max.

Syntax of "if then else" in C++ is thus pretty straight-forward:
if(boolean condition){
block of statements to be executed when condition is true;
}
else{
block of statements to be executed when condition is false;
}

NOTE(**): If there is only one statment to be written inside these blocks,
we can omit the curly braces ({ }), but it is suggested to use them
for better readability of code.

So, we can now continue with the program:
*/

int max; // to store the maximum out of the two

// conditional statement
if(num1 > num2){
max = num1;
}
else{
max = num2;
}

cout << "The maximum no. is: " << max << "\n";

/*
Note that the following piece of code can be replaced with lines 56 to 61
without affecting the working of the program (using Note **):

if(num1 > num2) max = num1;
else max = num2;
*/

// TRY IT YOURSELF:

/*
In this code, we used "if then else" conditional,
there are two other similar type of conditionals:
- "if" conditional
- "if else if" conditional

In the "if conditional", there is only the if part and the else part
is not required.

Try writing the same program using an "if conditional".

HINT: You can also initialize variables. for eg.

int number = num1;

The above line of code declares an integer variable named "number"
that is initialized with the value of the variable "num1", provided
"num1" is already declared above.
*/
return 0;
}

// Thank you. Keep Learning! See you in the next program!