Skip to content

Commit

Permalink
UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
SR-Sunny-Raj committed Nov 2, 2021
1 parent f0d8ff1 commit e46dac5
Show file tree
Hide file tree
Showing 63 changed files with 891 additions and 1,170 deletions.
54 changes: 0 additions & 54 deletions .gitignore

This file was deleted.

5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"NewtonRaphson.C": "cpp"
}
}
Binary file removed 04. Arrays${file_base_name}
Binary file not shown.
85 changes: 42 additions & 43 deletions 06. Sorting/cocktail_sort.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
# Python program for implementation of Cocktail Sort


def cocktailSort(a):
n = len(a)
# GitHub username: grvkr777 (Gaurav Kar)
# Python program for implementation of Cocktail Sort
def cocktailSort(a):
n = len(a)
swapped = True
start = 0
end = n-1
while (swapped == True):

# reset the swapped flag on entering the loop,
# because it might be true from a previous
# iteration.
while (swapped == True):
# reset the swapped flag on entering the loop,
# because it might be true from a previous
# iteration.
swapped = False

# loop from left to right same as the bubble
# sort
for i in range(start, end):
if (a[i] > a[i + 1]):
a[i], a[i + 1] = a[i + 1], a[i]
# loop from left to right same as the bubble
# sort
for i in range (start, end):
if (a[i] > a[i + 1]) :
a[i], a[i + 1]= a[i + 1], a[i]
swapped = True

# if nothing moved, then array is sorted.
if (swapped == False):
# if nothing moved, then array is sorted.
if (swapped == False):
break

# otherwise, reset the swapped flag so that it
# can be used in the next stage
# otherwise, reset the swapped flag so that it
# can be used in the next stage
swapped = False

# move the end point back by one, because
# item at the end is in its rightful spot
# move the end point back by one, because
# item at the end is in its rightful spot
end = end-1

# from right to left, doing the same
# comparison as in the previous stage
for i in range(end-1, start-1, -1):
if (a[i] > a[i + 1]):
a[i], a[i + 1] = a[i + 1], a[i]
# from right to left, doing the same
# comparison as in the previous stage
for i in range(end-1, start-1, -1):
if (a[i] > a[i + 1]):
a[i], a[i + 1] = a[i + 1], a[i]
swapped = True

# increase the starting point, because
# the last stage would have moved the next
# smallest number to its rightful spot.
# increase the starting point, because
# the last stage would have moved the next
# smallest number to its rightful spot.
start = start + 1


# Driver code
a = [5, 1, 4, 2, 8, 0, 2]
cocktailSort(a)
print("Sorted array is:")
for i in range(len(a)):
print("% d" % a[i])

# Driver code to test above
a = [5, 1, 4, 2, 8, 0, 2]
cocktailSort(a)
print("Sorted array is:")
for i in range(len(a)):
print ("% d" % a[i]),
105 changes: 46 additions & 59 deletions 06. Sorting/heapsort.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,52 @@
// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;

// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2

// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;

// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;

// If largest is not root
if (largest != i) {
swap(arr[i], arr[largest]);

// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
#include <iostream>
using namespace std;

void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;

if (r < n && arr[r] > arr[largest])
largest = r;

if (largest != i)
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void buildheap(int arr[],int n){
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
}

// main function to do heap sort
void heapSort(int arr[], int n)
{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
void heapSort(int arr[], int n)
{
buildheap(arr,n);

// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
for (int i=n-1; i>0; i--)
{
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}

/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << "\n";
}

// Driver program
int main()
int main(void)
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);

heapSort(arr, n);

cout << "Sorted array is \n";
printArray(arr, n);
}
int a[10];
cout << "Enter 10 elements of array: " << endl;
for (int i=0; i<10; i++)
cin >> a[i];
cout << "Original Array is: ";
for (int i=0; i<10; i++)
cout << a[i] << " ";
int n=sizeof(a)/sizeof(a[0]);
cout <<"\nArray after sorting is: ";
heapSort(a,n);
for(int x: a)
cout<<x<<" ";
}
76 changes: 38 additions & 38 deletions 01_Reverse_Array.cpp → 30. CPP Programs/01_Reverse_Array.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
// C++ program to reverse every sub-array formed by
// consecutive k elements
#include <iostream>
using namespace std;

// Function to reverse every sub-array formed by
// consecutive k elements
void reverse(int arr[], int n, int k)
{
for (int i = 0; i < n; i += k)
{
int left = i;

// to handle case when k is not multiple of n
int right = min(i + k - 1, n - 1);

// reverse the sub-array [left, right]
while (left < right)
swap(arr[left++], arr[right--]);

}
}

// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int k = 3;

int n = sizeof(arr) / sizeof(arr[0]);

reverse(arr, n, k);

for (int i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
}
// C++ program to reverse every sub-array formed by
// consecutive k elements
#include <iostream>
using namespace std;

// Function to reverse every sub-array formed by
// consecutive k elements
void reverse(int arr[], int n, int k)
{
for (int i = 0; i < n; i += k)
{
int left = i;

// to handle case when k is not multiple of n
int right = min(i + k - 1, n - 1);

// reverse the sub-array [left, right]
while (left < right)
swap(arr[left++], arr[right--]);

}
}

// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int k = 3;

int n = sizeof(arr) / sizeof(arr[0]);

reverse(arr, n, k);

for (int i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
}
42 changes: 21 additions & 21 deletions 1. Two sums.cpp → 30. CPP Programs/1. Two sums.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> ans = {};
unordered_map<int, int> hashMap;
for (int i = 0; i < nums.size(); i++)
{
if (hashMap.find(target - nums[i]) != hashMap.end())
{
ans.push_back(hashMap[target - nums[i]]);
ans.push_back(i);
return ans;
}
hashMap[nums[i]] = i;
}
return ans;
}
};
// https://leetcode.com/problems/two-sum/discuss/?currentPage=1&orderBy=hot&query=
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> ans = {};
unordered_map<int, int> hashMap;
for (int i = 0; i < nums.size(); i++)
{
if (hashMap.find(target - nums[i]) != hashMap.end())
{
ans.push_back(hashMap[target - nums[i]]);
ans.push_back(i);
return ans;
}
hashMap[nums[i]] = i;
}
return ans;
}
};
// https://leetcode.com/problems/two-sum/discuss/?currentPage=1&orderBy=hot&query=
Loading

0 comments on commit e46dac5

Please sign in to comment.