Online note taking app - Notes.ionotes

Fast | Easy | Short

Online Note Services - notes.io

#include <stdio.h> //its for some known functions (printf,scanf etc.)
#include <conio.h> //its for getch() command.

int binarySearch();
int linearSearch();
int interpolationSearch();
int getinAorder();
int arrayLength;

int main(){
printf("Welcome to Instructive Searching Algorithms Programn");

printf("We will work on these 3 algorithms today:Linear Search, Binary Search,Interpolation Searchn");

printf("These algorithms works on arrays.n");

printf("You need the give me some numeric inputs so I can create an array.n");

printf("How many numbers will you give me?n");
scanf("%d",&arrayLength);

printf("Okay, start nown");

int i,numericInput[arrayLength]; //
// I am taking
for(i=0;i<arrayLength;i++){ // the elements.
scanf("%d",&numericInput[i]); //
} //


options: printf("Now you must choosen");
printf("1-Linear Searchn");
printf("2-Binary Searchn");
printf("3-Interpolation Searchn");
int choice;
scanf("%d",&choice);

switch(choice){
case 1:
linearSearch(numericInput,arrayLength);
break;
case 2:
binarySearch(numericInput,arrayLength);
break;
case 3:
interpolationSearch(numericInput,arrayLength);
break;
default:
printf("Wrong choice try again, press anything to go choice menu");
getch();
system("cls"); // I want to clean screen for my next moves so I'm going to use this command.
goto options; //provides an unconditional jump from the 'goto' to a labeled statement in the same function.
break;

}

getch();

return 0;
}


int linearSearch(int numericInput[],int arrayLength ){


system("cls");
printf("This search algorithm is the simplest algorithm in those 3 algorithm.n");
printf("I will say 'LS' for linear search from nown");
printf("LS has simple logic, first it will take a number from you and it starts digging.n");
printf("Lets try, what you want to search?(You must give me registered number from array)n");
printf("----------------------------------------------------------------------------------------------n");
int number;
input: scanf("%d",&number);

printf("Now lets see what we can...You gave me %d im going to compare this number with my first array input:%dn",number,numericInput[0]);
if(number==numericInput[0]){ //usually we use for all the time
printf("What a luck! We find at first step, if you want to learn something you should give me another numbern"); //but I want to show somethings to
printf("----------------------------------------------------------------------------------------------n"); //user.Because of that I used 'if'.
goto input;
}
else{
printf("These two numbers is not equal so I want to check next one in the arrayn");
printf("Simply this is the logic for linear search, we usually use 'if' function in the 'for' function for control mechanismn");
printf("----------------------------------------------------------------------------------------------n");

}
int i,k=0;
for(i=1;i<arrayLength;i++){
if(number==numericInput[i]){
k=1;
printf("Now i did some digging in for function and i found, the number what you gave me matching with %d. member of the array.I hope you learned something see you soon.n",i+1);

}

}
if(k==0){
printf("I did some research with 'for' function and I cant find any result, the number what you gave me is not in the array, give me a real number pleasen");
goto input;

}
}

int binarySearch(int numericInput[],int arrayLength){
system("cls");
printf("This algorithm is simpler than Interpolation Search and it is works only on sorted array.n");
getinAorder(numericInput,arrayLength); //We called this function for doing our array sorted.
printf("I sorted our array in my mindn");
printf("It is something like that now:n");
printf("-------------------------------------n");
int i;
for(i=0;i<arrayLength;i++){
printf("%d.element: %dn",i+1,numericInput[i]);
}
printf("--------------------------------n");
printf("Binary Search has only one purpose: Divide and Conqueren");
printf("When it takes a number from you it will start digging, shall we?n");
printf("Give me a number(from array): ");
int number;
scanf("%d",&number);
printf("-------------------------------------------------------------------n");
printf("Algorithm has %d now, it will take a look middle of the array.n",number);

int middle =(arrayLength-1)/2;

printf("We have %d as a middle value if it fits with our number we will stop.n",numericInput[middle]);
printf("-------------------------------------------------------------------n");

int highest=arrayLength; //
int lowest=-1; // I did this because I want to take the edge values (array[0] and array[arrayLength]
int flag=0; // you can try if you want...add -1 to higher , and change lower to 0.

while(highest-lowest>1){
middle=(highest+lowest)/2;
if(numericInput[middle]==number){
flag=1;
printf("We found it, its our %d. element",middle+1);
break;
}
else if(numericInput[middle]<number){
printf("Our element is greater than middle indice value,so we will assign our middle indice value as lowest value and we will work on right subarray. n");
printf("So I assigned our middle to lowest value for creating subarray.n");
printf("-------------------------------------------------------------------n");
lowest=middle;
}
else if(numericInput[middle]>number){
printf("Our element is less than middle indice value,so we will assign our middle indice value as highest value and we will work on left subarray. n");
printf("So I assigned our middle to highest value for creating subarray ");
printf("-------------------------------------------------------------------n");
highest=middle;
}

}

if(flag==0){
printf("we cant found the searching element in our array...n");
}




}

int interpolationSearch(int numericInput[],int arrayLength ){
system("cls");
printf("This search method can be use on a sorted array in computer science.n ");
getinAorder(numericInput,arrayLength);
printf("It is something like that now:n");
printf("-------------------------------------n");
int i;
for(i=0;i<arrayLength;i++){
printf("%d.element: %dn",i+1,numericInput[i]);
}
printf("-------------------------------------n");
printf("We will use a formula for find middle indice in array.n");
printf("After getting a result we will compare with our element.n");
printf("If it fits we will stop algorithm; if its not we will continue with same method in subarraysn");
printf("Lets start:n");
printf("-------------------------------------------------------------------n");
printf("We must define 2 argument: 'left' and 'right' these arguments means starting(left) and finishing(right) indices in arrayn");
printf("Give me a number in our array:n");
int number;
scanf("%d",&number);
printf("-------------------------------------------------------------------n");
printf("So these are some arguments for formula:n");
printf("--------------------------------------n");
printf("left=0(first indice)n");
printf("right=%d(last indice)n",arrayLength-1);
printf("array[left]=%d(our first value)n",numericInput[0]);
printf("array[right]=%d(our last value)n",numericInput[arrayLength-1]);
printf("our element is =%dn",number);
printf("--------------------------------------n");
printf("Formula is :nmiddleIndice=left + ( (element-array[left])*(right-left) ) / (array[right]-array[left])n");
printf("Lets find our middle indice for comparingn");
printf("--------------------------------------n");
int left=0;
int right=arrayLength-1;
int aleft=numericInput[0];
int aright=numericInput[arrayLength-1];
int middle=left+((number-aleft)*(right-left))/(aright-aleft);
// middle =left + (((double)(right-left) /(aright - aleft))*(number - aleft)) ;
/*HOCAM ÜSTTEKİ FORMÜLÜ ÇOĞU YERDEN DOĞRULADIM BÖYLE GÖZÜKÜYOR.BEN KODU ÇALIŞTIRIP BELLİ BİR DİZİ GİRDİKTEN SONRA ARATMAK İSTEDİĞİM ELEMANI ALGORİTMA HER SEFERİNDE
İLK ADIMDA BULUYOR.NORMALDE MID POINTI BULUP ALT KUME OLUSTURUP FORMULDEN GENE MID POINT BULMALIYDI.OKULDA EGE HOCAYA GOSTERIRKEN KOD CALISTI EVIMDE CALISMADI ARKADASIMDA DA
DENEDIM ONDA DA CALISMADI CIKMAZDAYIM :( (KOD CALISTI DERKEN OLMASI GEREKEN GIBI UYGULADI MANTIGI RUN EDILIYOR KOD HER TURLU) */

printf("Our middle indice is %d,we will compare with our elementn",middle);

if (numericInput[middle] == number){
printf("Yes, we found at first step, congratz! Otherwise we will keep searching subarrays at the same way.n");
}

else{


for(i=0;i<arrayLength;i++){

middle=left+((number-aleft)*(right-left))/(aright-aleft);
// middle =left + (((double)(right-left) /(aright - aleft))*(number - aleft)) ;

if (numericInput[middle] == number){
printf("Yes, we finally found, congratz! Our element is %d.order in the array.n",middle+1);
break;
}

else{

if(numericInput[middle] < number ){
printf("Our element=%dn",number);
printf("Middle value=%dn",numericInput[middle]);
printf("Our element is greater than middle value so we will create a subarray with right side and keep doing same procedure until we find the correct resultn");
printf("--------------------------------n");
printf("I am assigned middle+1 value to left so I can create a right subarray.");
printf("--------------------------------n");
left = middle+1;
aleft= numericInput[left];
aright = numericInput[right];



}

else {
printf("Our element=%dn",number);
printf("Middle value=%dn",numericInput[middle]);
printf("Our element is les than middle value so we will create a subarray with left side and keep doing same procedure until we find the correct resultn");
printf("--------------------------------n");
printf("I am assigned middle-1 value to left so I can create a left subarray.n");
printf("--------------------------------n");
right = middle-1;
aleft = numericInput[left];
aright = numericInput[right];


}
}
}
}
}

int getinAorder(int numericInput[],int arrayLength ){ //bubble sort algorithm
int i,j;
for(i=1;i<arrayLength;i++){
int k=0;
for(j=0;j<arrayLength-i;j++){
if(numericInput[j]>numericInput[j+1]){

k=numericInput[j+1];
numericInput[j+1]=numericInput[j];
numericInput[j]=k;

}
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for 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 12 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.