NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

4.28
AA
//linear search it
#include<stdio.h>

int linear_search(int a[], int x, int n) {
int i;
for (i = 1; i < n; i++) {
if (a[i] == x) return i;
}
return -1;
}

int main() {
int a[] = {
10,
20,
30,
40,
50,
60,
70,
80,
90,
100
};
int result = linear_search(a, 100, 10);
if (result != -1)
printf("Element x is present at index %d", result);
else
printf("Element x is not present in array");
return 0;
}
-------------------------------------------
//linear search rec
#include<stdio.h>

int rec_search(int a[], int x, int n) {
int i;
if (n == 0) {
return -1;
} else {
if (a[n - 1]==x) {
return n - 1;
} else {
rec_search(a, x, n-1);
}

}

}

int main() {
int a[] = {
10,
20,
30,
40,
50,
60,
70,
80,
90,
100
};
int result = rec_search(a, 90, 10);
if (result != -1)
printf("Element x is present at index %d", result);
else
printf("Element x is not present in array");
return 0;
}
------------------------------------

//binary search iterative
#include<stdio.h>

int binary_search_iterative(int a[], int x, int n){
int L=0, H=n-1;
int M=(L+H)/2;
do{
if(a[M]==x) return M;
else if(x>a[M]) {
L=M+1;
M=(L+H)/2;
}else {
H=M-1;
M=(L+H)/2;
}
}while(L<=H);
return -1;
}

int main() {
int a[] = {
10,
20,
30,
40,
50,
60,
70,
80,
90,
100
};
int result=binary_search_iterative(a, 100, 10);
if (result != -1)
printf("Element x is present at index %d", result);
else
printf("Element x is not present in array");
return 0;
}
----------------------------------
//binary_search_recursive
#include<stdio.h>

int binary_search_recursive(int a[], int x, int L, int H) {
int M = (L + H) / 2;
if (a[M] == x) return M;
if (H < L) return -1;

if (a[M] < x) return binary_search_recursive(a, x, M + 1, H);
else return binary_search_recursive(a, x, L, M - 1);
}
int main() {
int a[] = {
10,
20,
80,
30,
60,
50,
100,
110,
130,
170
};
int result = binary_search_recursive(a, 130, 0, 10);
if (result != -1) {
printf("Element x is present at index %d", result);
} else printf("Element x is not present in array");
return 0;
}

----------------------------------------
//find max iterative
#include<stdio.h>

int max(int a[], int n) {
int max=a[0];
for (int i =0; i<n; ++i){
if (max<a[i]){
max=a[i];
}
}
return max;
}

int main() {
int a[] = {
10,
20,
30,
40,
50,
60,
70,
80,
90,

};
int result = max(a, 10);
if (result != -1)
printf("max %d", result);
else
printf("Element x is not present in array");
return 0;
}
---------------------------------------
//find max recursive
#include <stdio.h>
int find_max(int a[], int n){
if(n==1) return a[0];
int submax=find_max(a,n-1);
if(a[n-1]<submax) return submax;
else return a[n-1];
}
int main(){
int a[]={1,4,2,5,10,3,5,99,3};
printf("%d",find_max(a,9));
return 0;
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.