-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterview.c
More file actions
120 lines (98 loc) · 2.43 KB
/
Interview.c
File metadata and controls
120 lines (98 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<stdio.h>
static int reverse_number(int number) {
int rev = 0;
int remainder = 0; //Consider the number with 2 digits
while(number !=0) {
remainder = number % 10; //Always give me last digit
rev = rev * 10 + remainder; //If remainder*10 then gives me original number
number = number / 10; //Always give me new number excluding last digit
}
return rev;
}
static void revrse_string(char *str) {
if(*str)
{
revrse_string(str+1); //put in the stack and then print by pop
printf("%c", *str);
}
}
static int count_len(char *s) {
int cnt = 0;
while(*s) { //while(s) => we cannot write as as s is address while *s is content
//Address(s) cannot be null while content(*s) null
//In tree we root->next = NULL we are doing address NULL so we can use while(root)
s++;
cnt++;
}
return cnt;
}
void * new(){
return "Hello";
}
static void conquer(int ip_arr[],int low,int high);
static void divide(int ip_arr[],int low,int high);
static void divide(int ip_arr[],int low,int high) {
int mid;
if(low >= high) {
return;
}
else {
mid = (low + high)/2;
divide(ip_arr,low,mid);
divide(ip_arr,mid+1,high);
conquer(ip_arr,low,high);
}
}
static void conquer(int ip_arr[],int low,int high) {
int start = low;
int rt = high;
int mid = (low + high)/2;
int mid_half = mid + 1;
int lt = low;
int target[100]; // int *temp = (int *)malloc(sizeof(int)*array_size);
while(lt <= mid && mid_half <= rt ) {
if(ip_arr[lt] < ip_arr[mid_half]) {
//copy the left portion
target[low++] = ip_arr[lt++];
}else { //else if(ip_arr[lt] > ip_arr[mid_half]){
//copy the right portion
target[low++] = ip_arr[mid_half++];
}
}
//divide while loop in =====> (if/else + for)
if(lt > mid) { //one of the while condition (lt <= mid)
for(;mid_half <= rt;) {
target[low++] = ip_arr[mid_half++];
}
}else if(mid_half > rt) {
for(;lt <= mid;) {
target[low++] = ip_arr[lt++];
}
}
/**
* Copy the conquered array into original
*/
for(lt = start; lt<=high; lt++) {
ip_arr[lt] = target[lt];
}
}
static void merge_sort(int arr[],int size) {
divide(arr,0,size-1);
}
//int main() {
// int number = 1234;
// int rev_number = reverse_number(number);
// printf("%d \t",rev_number);
//
// revrse_string("Hello");
// number = count_len("Krishna");
// printf("%d \t",number);
//
// int arr[] = {10,92,8,14,22,40};
// int i;
// merge_sort(arr,6);
// printf("\n");
// for(i=0;i<=5;i++) {
// printf("%d \t",arr[i]);
// }
//}