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

Commit fb02671

Browse files
authored
Revert "Radix sort implementation in Python for issue #79" (#411)
1 parent 7b0a6d0 commit fb02671

File tree

100 files changed

+6307
-50
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+6307
-50
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
name: Create new issue
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: Hacktoberfest, Up-for-Grabs, good first issue, help wanted
6+
assignees: ''
7+
8+
---
9+
10+
* Please follow the [Contributing Guidelines](https://github.com/AshishOhri/Yet_Another_Algorithms_Repository/blob/master/CONTRIBUTING.md) & naming conventions in the guidelines accordingly. 🙂
11+
* Following naming guidelines is important to maintain consistency.😊
12+
* 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 )

.github/PULL_REQUEST_TEMPLATE.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
TICK ALL THE BOXES AFTER READING THEM (to tick them add tick inside the square brackets with no spaces like this [x])
2+
- [ ] I have read the <a href="https://github.com/yet-another-series/Yet_Another_Algorithms_Repository/blob/master/CONTRIBUTING.md">
3+
Contributing Guidelines</a> and naming conventions in the guidelines accordingly.
4+
- [ ] I have checked that same implementation or the same code does not exist
5+
- [ ] I have referenced the issue (#issue_number) if the issue for the code exists
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def max_min(li, left, right):
2+
global count
3+
if (left == right):
4+
return li[left],li[left]
5+
elif (right-left == 1):
6+
return max(li[left],li[right]), min(li[left],li[right])
7+
else:
8+
mid = int((left + right) / 2)
9+
max1,min1 = max_min(li, left, mid)
10+
max2,min2 = max_min(li, mid+1, right)
11+
return max(max1,max2),min(min1,min2)
12+
13+
li = [5,1,3,4,6,2]
14+
maxi,mini = max_min(li, 0, len(li)-1)
15+
print(maxi)
16+
print(mini)
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#define MAX 100005
3+
4+
long long int arr[MAX];
5+
6+
long long int binarySearch(long long int A[], long long int N, long long int ele) {
7+
long long int beg, mid, end, flag = -1;
8+
beg = 0;
9+
end = N - 1;
10+
while(beg <= end) {
11+
mid = (beg + end) / 2;
12+
if(A[mid] == ele) {
13+
flag = mid;
14+
break;
15+
} else if(A[mid] < ele) {
16+
beg = mid + 1;
17+
} else {
18+
end = mid - 1;
19+
}
20+
}
21+
return flag;
22+
}
23+
24+
int main() {
25+
long long int N, S;
26+
printf("\nEnter number of elements of array: ");
27+
scanf("%lld", &N);
28+
printf("\nEnter elements of array: ");
29+
for(long long int i = 0 ; i < N ; i++) {
30+
scanf("%lld", &arr[i]);
31+
}
32+
printf("\nEnter number of searches: ");
33+
scanf("%lld", &S);
34+
while(S) {
35+
long long int T;
36+
printf("\nEnter element to search for: ");
37+
scanf("%lld", &T);
38+
long long int f = -1;
39+
f = binarySearch(arr, N, T);
40+
if(f == -1) {
41+
printf("%lld not found!", T);
42+
} else {
43+
printf("%lld found at index %lld", T, f);
44+
}
45+
S--;
46+
}
47+
return 0;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
int l,n,t,h,flag=0,mid=0,s;
8+
vector<int> a;
9+
cin>>t;
10+
while(t--)
11+
{
12+
cin>>s;
13+
a.push_back(s);
14+
}
15+
cin>>n;
16+
l=0;
17+
h=a.size()-1;
18+
19+
while(l<h)
20+
{
21+
mid=(l+h)/2;
22+
if(a[mid]>n) {h=mid-1;}
23+
else if(a[mid]<n) {l=mid+1;}
24+
else {flag=1; break;}
25+
}
26+
if(flag==1)
27+
cout<<mid;
28+
29+
return 0;
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class BinarySearch {
2+
// Returns index of x if it is present in arr[l..
3+
// r], else return -1
4+
int binarySearch(int arr[], int l, int r, int x) {
5+
if (r>=l) {
6+
int mid = l + (r - l)/2;
7+
8+
// If the element is present at the
9+
// middle itself
10+
if (arr[mid] == x)
11+
return mid;
12+
13+
// If element is smaller than mid, then
14+
// it can only be present in left subarray
15+
if (arr[mid] > x)
16+
return binarySearch(arr, l, mid-1, x);
17+
18+
// Else the element can only be present
19+
// in right subarray
20+
return binarySearch(arr, mid+1, r, x);
21+
}
22+
23+
// We reach here when element is not present
24+
// in array
25+
return -1;
26+
}
27+
28+
public static void main(String args[]) {
29+
BinarySearch ob = new BinarySearch();
30+
int arr[] = {2,3,4,10,40};
31+
int n = arr.length;
32+
int x = 10;
33+
int result = ob.binarySearch(arr,0,n-1,x);
34+
if (result == -1)
35+
System.out.println("Element not present");
36+
else
37+
System.out.println("Element found at index " + result);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Sorted array
2+
const array = [1, 2, 3, 5, 7, 8, 9, 11, 14, 15, 16, 18];
3+
4+
// Random value
5+
const value = Math.round(Math.random() * 20);
6+
7+
console.log(`Searching ${value} in array [${array}]`);
8+
9+
const binarySearch = (array, value, start, end) => {
10+
11+
// Base condition
12+
if (start > end) {
13+
return -1;
14+
}
15+
16+
// Find the middle value
17+
const middle = Math.floor((start + end) / 2);
18+
19+
// Compare with number
20+
if (array[middle] === value) {
21+
return middle;
22+
}
23+
24+
// If element at middle is greater than value, search in the firts half of array
25+
if (array[middle] > value) {
26+
console.log(`Search in sub-array [${array.slice(start, middle - 1)}]`);
27+
return binarySearch(array, value, start, middle - 1);
28+
}
29+
else {
30+
// If element at midddle is smaller than value, search in the right half of array
31+
console.log(`Search in sub-array [${array.slice(middle + 1, end)}]`);
32+
return binarySearch(array, value, middle + 1, end);
33+
}
34+
35+
}
36+
37+
const position = binarySearch(array, value, 0, array.length - 1);
38+
39+
if (position >= 0) {
40+
console.log(`${value} found at index ${position}`);
41+
} else {
42+
console.log('Element not found');
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def binary_search(list_, val, asc=1):
2+
'''
3+
list_: sorted list with unique values
4+
val: value to search
5+
asc: asc==1 list is sorted in ascending order
6+
7+
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'.
8+
'''
9+
10+
lo = 0
11+
hi = (len(list_)-1)
12+
13+
while (lo <= hi):
14+
mid = int(lo + (hi-lo)/2)
15+
#print("mid", mid)
16+
if (list_[mid] == val):
17+
return mid
18+
elif (list_[mid] > val):
19+
if (asc == 1): # increasing
20+
hi = mid-1
21+
else :
22+
lo = mid+1
23+
elif (list_[mid] < val):
24+
if (asc == 1): # increasing
25+
lo = mid+1
26+
else :
27+
hi = mid-1
28+
return -1
29+
30+
31+
a = [14,15,16,17,18,19]
32+
a_= [19,18,17,16,15,14]
33+
34+
print(binary_search(a, 16))
35+
print(binary_search(a, 19))
36+
print(binary_search(a, 18))
37+
print(binary_search(a, 11))
38+
39+
print(binary_search(a_, 16, 0))
40+
print(binary_search(a_, 19, 0))
41+
print(binary_search(a_, 18, 0))
42+
print(binary_search(a_, 11, 0))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def binary_search(arr, l, r, x):
2+
""" Function to search number in a list in logn time"""
3+
while l <= r:
4+
mid = (l + r) // 2
5+
6+
# Check if x is present at mid
7+
if arr[mid] == x:
8+
return mid
9+
10+
# If x is greater, ignore left half
11+
elif arr[mid] < x:
12+
l = mid + 1
13+
14+
# If x is smaller, ignore right half
15+
else:
16+
r = mid - 1
17+
18+
# If we reach here, then the element
19+
# was not present
20+
return -1
21+
22+
arr = list(map(int, input("Enter the sorted array: ").split()))
23+
x = int(input("Enter the number to search: "))
24+
25+
# Function call
26+
result = binary_search(arr, 0, len(arr) - 1, x)
27+
28+
if result != -1:
29+
print("Element is present at position {}".format(result + 1))
30+
else:
31+
print("Element is not present in array")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdio.h>
2+
#define MAX 100005
3+
4+
long long int arr[MAX];
5+
6+
int bubbleSort(long long int A[], long long int N) {
7+
int flag = 1;
8+
for(long long int i = 0 ; i < N ; i++) {
9+
if(A[i] > A[i + 1]) {
10+
flag = 0;
11+
long long int temp;
12+
temp = A[i];
13+
A[i] = A[i + 1];
14+
A[i + 1] = temp;
15+
}
16+
}
17+
return flag;
18+
}
19+
20+
int main() {
21+
long long int N;
22+
int f = 0;
23+
printf("\nEnter number of elements of array : ");
24+
f = scanf("%lld", &N);
25+
if(!f)
26+
return -1;
27+
printf("\nEnter elements of array : ");
28+
for(long long int i = 0 ; i < N ; i++) {
29+
f = scanf("%lld", &arr[i]);
30+
if(!f)
31+
return -1;
32+
}
33+
int sorted = 0;
34+
while(!sorted) {
35+
sorted = bubbleSort(arr, N - 1);
36+
}
37+
printf("\nSorted array : ");
38+
for(long long int i = 0 ; i < N ; i++) {
39+
printf("%lld ", arr[i]);
40+
}
41+
return 0;
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// C++ program for implementation of Bubble sort
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
void swap(int *xp, int *yp)
6+
{
7+
int temp = *xp;
8+
*xp = *yp;
9+
*yp = temp;
10+
}
11+
12+
// A function to implement bubble sort
13+
void bubbleSort(int arr[], int n)
14+
{
15+
int i, j;
16+
for (i = 0; i < n-1; i++)
17+
18+
// Last i elements are already in place
19+
for (j = 0; j < n-i-1; j++)
20+
if (arr[j] > arr[j+1])
21+
swap(&arr[j], &arr[j+1]);
22+
}
23+
24+
/* Function to print an array */
25+
void printArray(int arr[], int size)
26+
{
27+
int i;
28+
for (i = 0; i < size; i++)
29+
cout << arr[i] << " ";
30+
cout << endl;
31+
}
32+
33+
// Driver code
34+
int main()
35+
{
36+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
37+
int n = sizeof(arr)/sizeof(arr[0]);
38+
bubbleSort(arr, n);
39+
cout<<"Sorted array: \n";
40+
printArray(arr, n);
41+
return 0;
42+
}

0 commit comments

Comments
 (0)