mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 13:31:21 +03:00
Merge branch 'master' into master
This commit is contained in:
commit
f361e8eabb
@ -1,6 +1,7 @@
|
||||
# C
|
||||
All Algorithms implemented in C
|
||||
|
||||
|
||||
## Computer Oriented Statistical Methods
|
||||
- Gauss_Elimination
|
||||
- Lagrange_Theorem
|
||||
|
68
Searches/fibonacciSearch.c
Normal file
68
Searches/fibonacciSearch.c
Normal file
@ -0,0 +1,68 @@
|
||||
int fibMonaccianSearch(int arr[], int x, int n)
|
||||
{
|
||||
/* Initialize fibonacci numbers */
|
||||
int fibMMm2 = 0; // (m-2)'th Fibonacci No.
|
||||
int fibMMm1 = 1; // (m-1)'th Fibonacci No.
|
||||
int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci
|
||||
|
||||
/* fibM is going to store the smallest Fibonacci
|
||||
Number greater than or equal to n */
|
||||
while (fibM < n)
|
||||
{
|
||||
fibMMm2 = fibMMm1;
|
||||
fibMMm1 = fibM;
|
||||
fibM = fibMMm2 + fibMMm1;
|
||||
}
|
||||
|
||||
// Marks the eliminated range from front
|
||||
int offset = -1;
|
||||
|
||||
/* while there are elements to be inspected. Note that
|
||||
we compare arr[fibMm2] with x. When fibM becomes 1,
|
||||
fibMm2 becomes 0 */
|
||||
while (fibM > 1)
|
||||
{
|
||||
// Check if fibMm2 is a valid location
|
||||
int i = min(offset+fibMMm2, n-1);
|
||||
|
||||
/* If x is greater than the value at index fibMm2,
|
||||
cut the subarray array from offset to i */
|
||||
if (arr[i] < x)
|
||||
{
|
||||
fibM = fibMMm1;
|
||||
fibMMm1 = fibMMm2;
|
||||
fibMMm2 = fibM - fibMMm1;
|
||||
offset = i;
|
||||
}
|
||||
|
||||
/* If x is greater than the value at index fibMm2,
|
||||
cut the subarray after i+1 */
|
||||
else if (arr[i] > x)
|
||||
{
|
||||
fibM = fibMMm2;
|
||||
fibMMm1 = fibMMm1 - fibMMm2;
|
||||
fibMMm2 = fibM - fibMMm1;
|
||||
}
|
||||
|
||||
/* element found. return index */
|
||||
else return i;
|
||||
}
|
||||
|
||||
/* comparing the last element with x */
|
||||
if(fibMMm1 && arr[offset+1]==x)return offset+1;
|
||||
|
||||
/*element not found. return -1 */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int arr[] = {10, 22, 35, 40, 45, 50, 80, 82,
|
||||
85, 90, 100};
|
||||
int n = sizeof(arr)/sizeof(arr[0]);
|
||||
int x = 85;
|
||||
printf("Found at index: %d",
|
||||
fibMonaccianSearch(arr, x, n));
|
||||
return 0;
|
||||
}
|
48
Sorts/shellSort.c
Normal file
48
Sorts/shellSort.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
void shellSort(int array[], int value){
|
||||
int i = value;
|
||||
int j, k, tmp;
|
||||
for (i = value / 2; i > 0; i = i / 2){
|
||||
for (j = i; j < value; j++){
|
||||
for(k = j - i; k >= 0; k = k - i){
|
||||
if (array[k+i] >= array[k]){
|
||||
break;
|
||||
}
|
||||
else{
|
||||
tmp = array[k];
|
||||
array[k] = array[k+i];
|
||||
array[k+i] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int array[20];
|
||||
int range = 500;
|
||||
for(int i = 0; i < 100; i++){
|
||||
array[i] = rand() % range + 1;
|
||||
}
|
||||
int size = sizeof array / sizeof array[0];
|
||||
|
||||
|
||||
|
||||
clock_t start = clock();
|
||||
shellSort(array,size);
|
||||
clock_t end = clock();
|
||||
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
|
||||
|
||||
printf("Data Sorted\n");
|
||||
printf("%s\n", "Shell Sort Big O Notation:\n--> Best Case: O(n log(n))\n--> Average Case: depends on gap sequence\n--> Worst Case: O(n)\n");
|
||||
printf("Time spent sorting: %f\n", time_spent);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
28
Untitled.c
Normal file
28
Untitled.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int i,n,l=0;
|
||||
scanf("%d",&n);
|
||||
int a[n];
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d",&a[i]);
|
||||
if(a[i]>l)
|
||||
l=a[i];
|
||||
}
|
||||
int b[l+1]={0};
|
||||
for(i=0;i<n;i++)
|
||||
b[a[i]]++; //hashing number to array index
|
||||
for(i=0;i<(l+1);i++)
|
||||
{
|
||||
if(b[i]>0)
|
||||
{
|
||||
while(b[i]!=0) //for case when number exists more than once
|
||||
{
|
||||
printf("%d ",i);
|
||||
b[i]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
27
searching/LinearSearch.c
Normal file
27
searching/LinearSearch.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int linearsearch(int *arr, int size, int val){
|
||||
int i;
|
||||
for (i = 0; i < size; i++){
|
||||
if (arr[i] == val)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void main(){
|
||||
int s,i,v;
|
||||
printf("Enter the size of the array:\n");
|
||||
scanf("%d",&s);
|
||||
|
||||
int a[s];
|
||||
printf("Enter the contents for an array of size %d:\n", s);
|
||||
for (i = 0; i < s; i++) scanf("%d", &a[i]);
|
||||
|
||||
printf("Enter the value to be searched:\n");
|
||||
scanf("%d", &v);
|
||||
if (linearsearch(a, s, v))
|
||||
printf("Value %d is in the array.\n", v);
|
||||
else
|
||||
printf("Value %d is not in the array.\n", v);
|
||||
}
|
Loading…
Reference in New Issue
Block a user