Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
Revert "Radix sort implementation in Python for issue #79" (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishOhri committed Nov 17, 2019
1 parent 7b0a6d0 commit fb02671
Show file tree
Hide file tree
Showing 100 changed files with 6,307 additions and 50 deletions.
12 changes: 12 additions & 0 deletions .github/ISSUE_TEMPLATE/create-new-issue.md
@@ -0,0 +1,12 @@
---
name: Create new issue
about: Describe this issue template's purpose here.
title: ''
labels: Hacktoberfest, Up-for-Grabs, good first issue, help wanted
assignees: ''

---

* Please follow the [Contributing Guidelines](https://github.com/AshishOhri/Yet_Another_Algorithms_Repository/blob/master/CONTRIBUTING.md) & naming conventions in the guidelines accordingly. 🙂
* Following naming guidelines is important to maintain consistency.😊
* Please *NOTE*: In case, same implementation of an algorithm is submitted by someone else and it gets accepted, then your contribution won't be merged ( to avoid duplicates )
5 changes: 5 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,5 @@
TICK ALL THE BOXES AFTER READING THEM (to tick them add tick inside the square brackets with no spaces like this [x])
- [ ] I have read the <a href="https://github.com/yet-another-series/Yet_Another_Algorithms_Repository/blob/master/CONTRIBUTING.md">
Contributing Guidelines</a> and naming conventions in the guidelines accordingly.
- [ ] I have checked that same implementation or the same code does not exist
- [ ] I have referenced the issue (#issue_number) if the issue for the code exists
@@ -0,0 +1,17 @@
def max_min(li, left, right):
global count
if (left == right):
return li[left],li[left]
elif (right-left == 1):
return max(li[left],li[right]), min(li[left],li[right])
else:
mid = int((left + right) / 2)
max1,min1 = max_min(li, left, mid)
max2,min2 = max_min(li, mid+1, right)
return max(max1,max2),min(min1,min2)

li = [5,1,3,4,6,2]
maxi,mini = max_min(li, 0, len(li)-1)
print(maxi)
print(mini)

48 changes: 48 additions & 0 deletions Algorithms/binary_search/c-binary_search-O(log N).c
@@ -0,0 +1,48 @@
#include <stdio.h>
#define MAX 100005

long long int arr[MAX];

long long int binarySearch(long long int A[], long long int N, long long int ele) {
long long int beg, mid, end, flag = -1;
beg = 0;
end = N - 1;
while(beg <= end) {
mid = (beg + end) / 2;
if(A[mid] == ele) {
flag = mid;
break;
} else if(A[mid] < ele) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
return flag;
}

int main() {
long long int N, S;
printf("\nEnter number of elements of array: ");
scanf("%lld", &N);
printf("\nEnter elements of array: ");
for(long long int i = 0 ; i < N ; i++) {
scanf("%lld", &arr[i]);
}
printf("\nEnter number of searches: ");
scanf("%lld", &S);
while(S) {
long long int T;
printf("\nEnter element to search for: ");
scanf("%lld", &T);
long long int f = -1;
f = binarySearch(arr, N, T);
if(f == -1) {
printf("%lld not found!", T);
} else {
printf("%lld found at index %lld", T, f);
}
S--;
}
return 0;
}
30 changes: 30 additions & 0 deletions Algorithms/binary_search/cpp-binary_search-O(log_n).cpp
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>

using namespace std;

int main()
{
int l,n,t,h,flag=0,mid=0,s;
vector<int> a;
cin>>t;
while(t--)
{
cin>>s;
a.push_back(s);
}
cin>>n;
l=0;
h=a.size()-1;

while(l<h)
{
mid=(l+h)/2;
if(a[mid]>n) {h=mid-1;}
else if(a[mid]<n) {l=mid+1;}
else {flag=1; break;}
}
if(flag==1)
cout<<mid;

return 0;
}
39 changes: 39 additions & 0 deletions Algorithms/binary_search/java-binary_search-O(logN).java
@@ -0,0 +1,39 @@
public class BinarySearch {
// Returns index of x if it is present in arr[l..
// r], else return -1
int binarySearch(int arr[], int l, int r, int x) {
if (r>=l) {
int mid = l + (r - l)/2;

// If the element is present at the
// middle itself
if (arr[mid] == x)
return mid;

// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);

// Else the element can only be present
// in right subarray
return binarySearch(arr, mid+1, r, x);
}

// We reach here when element is not present
// in array
return -1;
}

public static void main(String args[]) {
BinarySearch ob = new BinarySearch();
int arr[] = {2,3,4,10,40};
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr,0,n-1,x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}
43 changes: 43 additions & 0 deletions Algorithms/binary_search/js-binary_search-O(log-n).js
@@ -0,0 +1,43 @@
// Sorted array
const array = [1, 2, 3, 5, 7, 8, 9, 11, 14, 15, 16, 18];

// Random value
const value = Math.round(Math.random() * 20);

console.log(`Searching ${value} in array [${array}]`);

const binarySearch = (array, value, start, end) => {

// Base condition
if (start > end) {
return -1;
}

// Find the middle value
const middle = Math.floor((start + end) / 2);

// Compare with number
if (array[middle] === value) {
return middle;
}

// If element at middle is greater than value, search in the firts half of array
if (array[middle] > value) {
console.log(`Search in sub-array [${array.slice(start, middle - 1)}]`);
return binarySearch(array, value, start, middle - 1);
}
else {
// If element at midddle is smaller than value, search in the right half of array
console.log(`Search in sub-array [${array.slice(middle + 1, end)}]`);
return binarySearch(array, value, middle + 1, end);
}

}

const position = binarySearch(array, value, 0, array.length - 1);

if (position >= 0) {
console.log(`${value} found at index ${position}`);
} else {
console.log('Element not found');
}
42 changes: 42 additions & 0 deletions Algorithms/binary_search/python-binary_search-O(log(n)).py
@@ -0,0 +1,42 @@
def binary_search(list_, val, asc=1):
'''
list_: sorted list with unique values
val: value to search
asc: asc==1 list is sorted in ascending order
Searches for a given element in a list sorted in ascending order. For searching in a list sorted in descending order, pass the argument value for asc as '0'.
'''

lo = 0
hi = (len(list_)-1)

while (lo <= hi):
mid = int(lo + (hi-lo)/2)
#print("mid", mid)
if (list_[mid] == val):
return mid
elif (list_[mid] > val):
if (asc == 1): # increasing
hi = mid-1
else :
lo = mid+1
elif (list_[mid] < val):
if (asc == 1): # increasing
lo = mid+1
else :
hi = mid-1
return -1


a = [14,15,16,17,18,19]
a_= [19,18,17,16,15,14]

print(binary_search(a, 16))
print(binary_search(a, 19))
print(binary_search(a, 18))
print(binary_search(a, 11))

print(binary_search(a_, 16, 0))
print(binary_search(a_, 19, 0))
print(binary_search(a_, 18, 0))
print(binary_search(a_, 11, 0))
31 changes: 31 additions & 0 deletions Algorithms/binary_search/python-binary_search-O(logn).py
@@ -0,0 +1,31 @@
def binary_search(arr, l, r, x):
""" Function to search number in a list in logn time"""
while l <= r:
mid = (l + r) // 2

# Check if x is present at mid
if arr[mid] == x:
return mid

# If x is greater, ignore left half
elif arr[mid] < x:
l = mid + 1

# If x is smaller, ignore right half
else:
r = mid - 1

# If we reach here, then the element
# was not present
return -1

arr = list(map(int, input("Enter the sorted array: ").split()))
x = int(input("Enter the number to search: "))

# Function call
result = binary_search(arr, 0, len(arr) - 1, x)

if result != -1:
print("Element is present at position {}".format(result + 1))
else:
print("Element is not present in array")
42 changes: 42 additions & 0 deletions Algorithms/bubble_sort/c-bubble_sort-O(n^2).c
@@ -0,0 +1,42 @@
#include <stdio.h>
#define MAX 100005

long long int arr[MAX];

int bubbleSort(long long int A[], long long int N) {
int flag = 1;
for(long long int i = 0 ; i < N ; i++) {
if(A[i] > A[i + 1]) {
flag = 0;
long long int temp;
temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
}
}
return flag;
}

int main() {
long long int N;
int f = 0;
printf("\nEnter number of elements of array : ");
f = scanf("%lld", &N);
if(!f)
return -1;
printf("\nEnter elements of array : ");
for(long long int i = 0 ; i < N ; i++) {
f = scanf("%lld", &arr[i]);
if(!f)
return -1;
}
int sorted = 0;
while(!sorted) {
sorted = bubbleSort(arr, N - 1);
}
printf("\nSorted array : ");
for(long long int i = 0 ; i < N ; i++) {
printf("%lld ", arr[i]);
}
return 0;
}
42 changes: 42 additions & 0 deletions Algorithms/bubble_sort/cpp-bubble_sort-O(n^2).cpp
@@ -0,0 +1,42 @@
// C++ program for implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

// Driver code
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}

0 comments on commit fb02671

Please sign in to comment.