Skip to content

Commit c59c98c

Browse files
authored
Merge pull request codeIIEST#23 from prateekiiest/master
Create Overall.
2 parents 85a3340 + f32d144 commit c59c98c

File tree

7 files changed

+311
-0
lines changed

7 files changed

+311
-0
lines changed

search/Binary_Search.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<stdio.h>
2+
int main() {
3+
4+
int n, a[30], item, i, j, mid, top, bottom;
5+
6+
7+
printf("Enter how many elements you want:\n"); // no of elements
8+
scanf("%d", &n);
9+
printf("Enter the %d elements in ascending order\n", n);
10+
for (i = 0; i < n; i++) {
11+
scanf("%d", &a[i]);
12+
}
13+
14+
printf("\nEnter the item to search\n"); // Target element to be searched
15+
scanf("%d", &item);
16+
bottom = 1;
17+
top = n;
18+
19+
do {
20+
mid = (bottom + top) / 2;
21+
if (item < a[mid])
22+
top = mid - 1; // Here we are dividing the array into two equal parts
23+
else if (item > a[mid]) /* if target element > mid part of array , we do the search in the upper part of the array
24+
else search in the lower part */
25+
bottom = mid + 1;
26+
} while (item != a[mid] && bottom <= top);
27+
28+
if (item == a[mid]) {
29+
printf("Binary search successfull!!\n");
30+
printf("\n %d found in position: %d\n", item, mid + 1);
31+
} else {
32+
printf("\n Search failed\n %d not found\n", item);
33+
}
34+
return 0;
35+
}

search/linear_search.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
int array[100], search, c, n;
6+
7+
printf("Enter the number of elements in array\n");
8+
scanf("%d",&n); // Total no of elements
9+
10+
printf("Enter %d integer(s)\n", n);
11+
12+
for (c = 0; c < n; c++)
13+
scanf("%d", &array[c]); // Reading the elements
14+
15+
printf("Enter the number to search\n"); // Target element to be searched
16+
scanf("%d", &search);
17+
18+
for (c = 0; c < n; c++)
19+
{
20+
if (array[c] == search) /* if required element found */
21+
{
22+
printf("%d is present at location %d.\n", search, c+1);
23+
break;
24+
}
25+
}
26+
if (c == n)
27+
printf("%d is not present in array.\n", search); // Element not found
28+
29+
return 0;
30+
}

sort/Merge_Sort.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Merge sort in C */
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
5+
// Function to Merge Arrays L and R into A.
6+
// lefCount = number of elements in L
7+
// rightCount = number of elements in R.
8+
void Merge(int *A,int *L,int leftCount,int *R,int rightCount) {
9+
int i,j,k;
10+
11+
// i - to mark the index of left aubarray (L)
12+
// j - to mark the index of right sub-raay (R)
13+
// k - to mark the index of merged subarray (A)
14+
i = 0; j = 0; k =0;
15+
16+
while(i<leftCount && j< rightCount) {
17+
if(L[i] < R[j]) A[k++] = L[i++];
18+
else A[k++] = R[j++];
19+
}
20+
while(i < leftCount) A[k++] = L[i++];
21+
while(j < rightCount) A[k++] = R[j++];
22+
}
23+
24+
// Recursive function to sort an array of integers.
25+
void MergeSort(int *A,int n) {
26+
int mid,i, *L, *R;
27+
if(n < 2) return; // base condition. If the array has less than two element, do nothing.
28+
29+
mid = n/2; // find the mid index.
30+
31+
// create left and right subarrays
32+
// mid elements (from index 0 till mid-1) should be part of left sub-array
33+
// and (n-mid) elements (from mid to n-1) will be part of right sub-array
34+
L = (int*)malloc(mid*sizeof(int));
35+
R = (int*)malloc((n- mid)*sizeof(int));
36+
37+
for(i = 0;i<mid;i++) L[i] = A[i]; // creating left subarray
38+
for(i = mid;i<n;i++) R[i-mid] = A[i]; // creating right subarray
39+
40+
MergeSort(L,mid); // sorting the left subarray
41+
MergeSort(R,n-mid); // sorting the right subarray
42+
Merge(A,L,mid,R,n-mid); // Merging L and R into A as sorted list.
43+
free(L);
44+
free(R);
45+
}
46+
47+
int main() {
48+
/* Code to test the MergeSort function. */
49+
50+
int A[] = {6,2,3,1,9,10,15,13,12,17}; // creating an array of integers.
51+
int i,numberOfElements;
52+
53+
// finding number of elements in array as size of complete array in bytes divided by size of integer in bytes.
54+
// This won't work if array is passed to the function because array
55+
// is always passed by reference through a pointer. So sizeOf function will give size of pointer and not the array.
56+
// Watch this video to understand this concept - http://www.youtube.com/watch?v=CpjVucvAc3g
57+
numberOfElements = sizeof(A)/sizeof(A[0]);
58+
59+
// Calling merge sort to sort the array.
60+
MergeSort(A,numberOfElements);
61+
62+
//printing all elements in the array once its sorted.
63+
for(i = 0;i < numberOfElements;i++) printf("%d ",A[i]);
64+
return 0;
65+
}

sort/Quick_Sort.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
5+
//quick Sort function to Sort Integer array list
6+
void quicksort(int array[], int firstIndex, int lastIndex)
7+
{
8+
//declaaring index variables
9+
int pivotIndex, temp, index1, index2;
10+
11+
if(firstIndex < lastIndex)
12+
{
13+
//assigning first element index as pivot element
14+
pivotIndex = firstIndex;
15+
index1 = firstIndex;
16+
index2 = lastIndex;
17+
18+
//Sorting in Ascending order with quick sort
19+
while(index1 < index2)
20+
{
21+
while(array[index1] <= array[pivotIndex] && index1 < lastIndex)
22+
{
23+
index1++;
24+
}
25+
while(array[index2]>array[pivotIndex])
26+
{
27+
index2--;
28+
}
29+
30+
if(index1<index2)
31+
{
32+
//Swapping opertation
33+
temp = array[index1];
34+
array[index1] = array[index2];
35+
array[index2] = temp;
36+
}
37+
}
38+
39+
//At the end of first iteration, swap pivot element with index2 element
40+
temp = array[pivotIndex];
41+
array[pivotIndex] = array[index2];
42+
array[index2] = temp;
43+
44+
//Recursive call for quick sort, with partiontioning
45+
quicksort(array, firstIndex, index2-1);
46+
quicksort(array, index2+1, lastIndex);
47+
}
48+
}
49+
50+
int main()
51+
{
52+
//Declaring variables
53+
int array[100],n,i;
54+
55+
//Number of elements in array form user input
56+
printf("Enter the number of element you want to Sort : ");
57+
scanf("%d",&n);
58+
59+
//code to ask to enter elements from user equal to n
60+
printf("Enter Elements in the list : ");
61+
for(i = 0; i < n; i++)
62+
{
63+
scanf("%d",&array[i]);
64+
}
65+
66+
//calling quickSort function defined above
67+
quicksort(array,0,n-1);
68+
69+
//print sorted array
70+
printf("Sorted elements: ");
71+
for(i=0;i<n;i++)
72+
printf(" %d",array[i]);
73+
74+
getch();
75+
return 0;
76+
}

sort/bubble_sort.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
// BUBBLE SORT
3+
4+
#include <stdio.h>
5+
6+
7+
void bubblesort(int arr[], int size)
8+
{
9+
int i, j;
10+
for (i = 0; i < size; i++) // Function where the actual algorithm is implemented
11+
{
12+
for (j = 0; j < size - i; j++)
13+
{
14+
if (arr[j] > arr[j+1])
15+
swap(&arr[j], &arr[j+1]);
16+
17+
}
18+
}
19+
}
20+
void swap(int *a, int *b)
21+
{
22+
int temp; // Function for swapping two variables
23+
temp = *a;
24+
*a = *b;
25+
*b = temp;
26+
}
27+
int main()
28+
{
29+
int array[100], i, size;
30+
printf("How many numbers you want to sort: "); // Enter the numbers to sort
31+
32+
scanf("%d", &size);
33+
34+
printf("\nEnter %d numbers : ", size);
35+
for (i = 0; i < size; i++)
36+
scanf("%d", &array[i]);
37+
bubblesort(array, size);
38+
printf("\nSorted array is ");
39+
40+
for (i = 0; i < size; i++)
41+
printf(" %d ", array[i]);
42+
return 0;
43+
44+
}

sort/insertion_sort.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// INSERTION SORT
2+
3+
#include<stdio.h>
4+
5+
int main(){
6+
7+
int i,j,s,temp,a[20];
8+
9+
printf("Enter total elements: ");
10+
scanf("%d",&s);
11+
12+
printf("Enter %d elements: ",s);
13+
for(i=0;i<s;i++)
14+
scanf("%d",&a[i]);
15+
16+
for(i=1;i<s;i++){
17+
temp=a[i];
18+
j=i-1;
19+
while((temp<a[j])&&(j>=0)){
20+
a[j+1]=a[j];
21+
j=j-1;
22+
}
23+
a[j+1]=temp;
24+
}
25+
26+
printf("After sorting: ");
27+
for(i=0;i<s;i++)
28+
printf(" %d",a[i]);
29+
30+
return 0;
31+
}

sort/selection_sort.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Selection Sort
2+
3+
#include<stdio.h>
4+
int main(){
5+
6+
int s,i,j,temp,a[20];
7+
8+
printf("Enter total elements: "); // total no of elements
9+
scanf("%d",&s);
10+
11+
printf("Enter %d elements: ",s); // the elements
12+
for(i=0;i<s;i++)
13+
scanf("%d",&a[i]);
14+
15+
for(i=0;i<s;i++){
16+
for(j=i+1;j<s;j++){
17+
if(a[i]>a[j]){
18+
temp=a[i]; // Compare between 2 consecutive elements and swap them if they are not in ascending order
19+
a[i]=a[j];
20+
a[j]=temp;
21+
}
22+
}
23+
}
24+
25+
printf("After sorting is: ");
26+
for(i=0;i<s;i++) // Print the sorted array
27+
printf(" %d",a[i]);
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)