-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from aniketsharma00411/master
Rebase
- Loading branch information
Showing
6 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// kadane's Algorithm is used to find maximum subarray | ||
// contributed by devanshi katyal | ||
// Time complexity:O(n) as array is iterated only once | ||
// Space complexity: O(1) since no extra space is being used | ||
|
||
#include <iostream> | ||
using namespace std; | ||
int kadanesAlgo(int *a, int n){ | ||
int global_sum=a[0], currsum=a[0]; | ||
for(int i=1;i<n;i++){ | ||
// current sum is maximum of the ith element or previous current sum + ith element | ||
currsum= max(a[i], currsum+a[i]); | ||
// if current sum is more than the global sum, then global sum will be given value of current sum | ||
if(currsum>global_sum){ | ||
global_sum= currsum; | ||
} | ||
} | ||
return global_sum; | ||
} | ||
int main() { | ||
int n; | ||
cin>>n; | ||
int a[n]; | ||
for(int j=0;j<n;j++){ | ||
cin>>a[j]; | ||
} | ||
int ans= kadane(a,n); | ||
cout<<ans; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
#include "list.h" | ||
using namespace std; | ||
|
||
void List::initialize() { | ||
last = -1; | ||
} | ||
|
||
bool List::isEmpty() { | ||
return last==-1 ; | ||
} | ||
|
||
bool List::isFull() { | ||
return last==499; | ||
} | ||
|
||
void List::insertData(const int& p, const int& e) { | ||
if(p < -1 or p>last) { | ||
throw ListException("Position invalid to insert"); | ||
|
||
} | ||
int i = last + 1; | ||
while(i>p) { | ||
data[i]=data[i-1]; | ||
|
||
i--; | ||
} | ||
data[p + 1]= e; | ||
last++; | ||
} | ||
|
||
void List::deleteData(const int&p) { | ||
if(p<0 or p>last) { | ||
throw ListException("Position invalid to delete"); | ||
return; | ||
} | ||
int i=p; | ||
while(i<last) { | ||
data[i]=data[i+1]; | ||
i++; | ||
} | ||
last--; | ||
|
||
} | ||
|
||
int List::getFirstPos() { | ||
if(isEmpty()) { | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
int List::getLastPos() { | ||
return last; | ||
|
||
} | ||
|
||
int List::getPrev(const int&p) { | ||
if(isEmpty()or p<1 or p>last) { | ||
return -1; | ||
} | ||
return p; | ||
|
||
} | ||
|
||
int List::getNext(const int&p) { | ||
if(isEmpty()or p<0 or p>last) { | ||
return -1; | ||
} | ||
return p+1; | ||
|
||
} | ||
|
||
int List::findData(const int& e) { ///Lineal Search | ||
int i=0; | ||
int j=last; | ||
int medio; | ||
while (i<=j) { | ||
medio=(i+j)/2; | ||
|
||
if(data[medio]== e) { | ||
return medio; | ||
} | ||
if(e<data[medio]) { | ||
j=medio - 1; | ||
} | ||
else { | ||
i=medio+1; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
int List::retrieve(const int&p) { | ||
if(isEmpty()or p<0 or p>last) { | ||
throw ListException("Non-existent element"); | ||
} | ||
return data[p]; | ||
|
||
} | ||
void List::exchange(int&a, int&b) { | ||
int aux(a); | ||
a=b; | ||
b=aux; | ||
|
||
} | ||
|
||
void List::sortData() { | ||
int i = 1,j,menor; | ||
while(i<last) { | ||
menor = i; | ||
|
||
j=i+1; | ||
while(j<=last) { | ||
if(data[j]<data[menor]) { | ||
menor=j; | ||
} | ||
j++; | ||
} | ||
if(menor!=i) { | ||
exchange(data[i],data[menor]); | ||
} | ||
|
||
i++; | ||
} | ||
} | ||
|
||
void List::print() { | ||
int i=0; | ||
while(i<=last) { | ||
std::cout << data[i]<<std::endl; | ||
i++; | ||
} | ||
|
||
} | ||
|
||
void List::deleteAll() { | ||
last = -1; | ||
} | ||
void List::writeToDisk(std::string fileName) { | ||
std::ofstream myFile; | ||
|
||
myFile.open(fileName,myFile.trunc); ///ios_base::trunc | ||
if(myFile.is_open()) { | ||
throw ListException("Could not open the file"); | ||
} | ||
|
||
int i=0; | ||
while(i<=last) { | ||
myFile<< data[i++]<<std::endl; | ||
} | ||
myFile.clear(); | ||
|
||
} | ||
void List::sortDataMerge() { | ||
sortDataMerge(0,last); | ||
} | ||
void List::quickSort(int leftEdge, int rightEdge) | ||
{ | ||
if(leftEdge<=rightEdge){ | ||
return; | ||
} | ||
|
||
int i=leftEdge, j=rightEdge; | ||
while(i<j){ | ||
while(i<j and data[i]<= data[rightEdge]){ | ||
i++; | ||
} | ||
while(i<j and data[j]>=data[rightEdge]){ | ||
j--; | ||
} | ||
if(i!=j){ | ||
exchange(data[i],data[j]); | ||
} | ||
} | ||
if(i!=rightEdge){ | ||
exchange(data[i],data[rightEdge]); | ||
} | ||
quickSort(leftEdge,i); | ||
quickSort(i+1,rightEdge); | ||
} | ||
|
||
void List::quickSort() | ||
{ | ||
quickSort(0,last); | ||
} | ||
|
||
|
||
|
||
|
||
void List::sortDataMerge(int leftEdge, int rightEdge) { | ||
if(leftEdge>=rightEdge) { | ||
return; | ||
} | ||
|
||
int mid=(leftEdge+rightEdge)/2; | ||
|
||
sortDataMerge(leftEdge,mid); | ||
sortDataMerge(mid+1,rightEdge); | ||
|
||
static int temp[500];///Tipo T, static siempre el mismo | ||
for(int i=leftEdge; i<=rightEdge; i++) { | ||
|
||
temp[i]=data[i]; | ||
} | ||
int i=leftEdge, j=mid+1, x=leftEdge; | ||
|
||
while(i<= mid and j<=rightEdge) { | ||
while(i<=mid and temp[i]<=temp[j]) { | ||
data[x++]=temp[i++]; | ||
} | ||
if(i<= mid) { | ||
while(j<=rightEdge and temp[j]<= temp[i]) { | ||
data[x++]=temp[j++]; | ||
} | ||
} | ||
} | ||
while(i<=mid) { | ||
data[x++]=temp[i++]; | ||
} | ||
while(j<=rightEdge) { | ||
data[x++]=temp[j++]; | ||
} | ||
|
||
} | ||
|
||
void List::readFromDisk(std::string) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Dart | ||
|
||
Common Algorithms which are usually taught in Data Structures and Algorithms courses. | ||
|
||
Make sure that they are in Dart and well commented. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Unit Tests for Dart Algorithms | ||
|
||
Unit Tests are needed for programs which are already present. | ||
|
||
Add atlest 5 unit tests per program. | ||
|
||
### File Naming | ||
|
||
Let there is a program named abcd.dart, its Unit Test should be named abcd_test.dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
void selectSorting(List list) { | ||
if (list == null || list.length == 0) return; | ||
int num = list.length; | ||
int i, steps; | ||
for (steps = 0; steps < num; steps++) { | ||
for (i = steps + 1; i < num; i++) { | ||
if(list[steps] > list[i]) { | ||
change(list, steps, i); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void change(List list, int steps, int i) { | ||
int count = list[steps]; | ||
list[steps] = list[i]; | ||
list[i] = count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.io.*; | ||
|
||
class BinarySearch { | ||
static int search(int arr[], int value, int leftIndex, int rightIndex) { | ||
// base case | ||
if (rightIndex < leftIndex) { | ||
return -1; | ||
} | ||
|
||
// calculate the mid index | ||
int midIndex = (rightIndex - leftIndex) / 2; | ||
midIndex += leftIndex; | ||
|
||
// if value is same as value on midIndex, simply return midIndex | ||
if (value == arr[midIndex]) { | ||
return midIndex; | ||
} else if (value < arr[midIndex]) { | ||
// when value is less than middle value, we search in the left half | ||
return search(arr, value, leftIndex, midIndex - 1); | ||
} else { | ||
// when value is greater than middle value, we search in the right half | ||
return search(arr, value, midIndex + 1, rightIndex); | ||
} | ||
} | ||
|
||
public static void main(String args[]) { | ||
// sorted array to search the value in | ||
int arr[] = { 10, 13, 15, 22, 89, 90 }; | ||
// value to search for | ||
int value = 15; | ||
|
||
// find the value | ||
int result = search(arr, value, 0, arr.length - 1); | ||
|
||
// show the result | ||
if (result == -1) { | ||
System.out.println("Item not found"); | ||
} else { | ||
System.out.println("Item found at index: " + result); | ||
} | ||
} | ||
} |