Algorithms

A code is a representation of an algorithm.
Exercises in the CS50 codespace, folder lec3algo
Attachments/Pasted image 20260512135447.png
X-axis = Problem size
Y-axis = Time to computer

Time complexity:

Searching from left to right OR from right to left in an array.

Example of linear search implementation with numbers:
linearSearch.c

#include <stdio.h>
#include <cs50.h>
int main(void)
{
	// Create an array of numbers
	
	int numbers[] = {1,20,30,6,50,10,230,100,500,25};
	int n = get_int("Number: ");
	
	for (int i = 0; i < 10; i++)
	{
	if (n == numbers[i])
	{	
		printf("number found\n");
		return 0;
	}
	
	}
	printf("not found\n");
	return 1;
}

Example of linear search implementation with strings:

betterLinearSearch.

#include <stdio.h>
#include <cs50.h>
int main(void)
{
	// Create an array of strings
    string strings[] ={"cat","dog","penguin","iron","cannon","boot"};
    string s = get_string("What are you looking for? \n");

    for (int i = 0; i < 6; i++){
        // Using string compare from the string library
        // strcmp takes 2 arguments
        // it returns 0 if found
        // returns positive or negative number if not found
        if (strcmp(strings[i],s) == 0)
        {
            printf("Found!\n");
            return 1;
        }
    }
    printf("not found...\n");
}
Data types:

Define a new data type that is a data structure with keywords : typedef struct

typedef struct
{
	string name;
	string age;
}
person;
#include <stdio.h>
#include <cs50.h>
typedef struct
{
	string name;
	string age;
}
person;

int main(void)
{
	person people[3]
}

We define an array of people with the relative name and numbers:

#include <stdio.h>
#include <cs50.h>
typedef struct
{
	string name;
	string age;
}
person;

int main(void)
{
	person people[3]
	
	people[0].name = "David";
	people[0].number = "20";
	
	people[1].name = "Luca";
	people[1].number = "22";
	
	people[2].name = "Sara";
	people[2].number = "18";
}

Implement the search algorithm:

#include <stdio.h>
#include <cs50.h>
#inlcude <string.h>

typedef struct
{
	string names;
	string age;
}
person;

int main(void)
{
	person people[3]
	
	people[0].names = "David";
	people[0].number = "20";
	
	people[1].names = "Luca";
	people[1].number = "22";
	
	people[2].names = "Sara";
	people[2].number = "18";
	
	string name = get_string("Name: ");
	
    for (int i = 0; i < 3; i++)
    {
        if (strcmp(people[i].names, name) == 0)
        {
            printf("%s\n", people[i].age);
            return 0;
        }
    }
    printf("Not found\n");
    return 1;
}

Binary search is a highly efficient algorithm used to find a specific item in a sorted list by repeatedly dividing the search space in half.

Attachments/Pasted image 20260614113659.png

Check sorting algorithms visually here

pseudocode for a selection sort
O(n^2)

For i from n to n-1 
	find smallest number between numbers[i] and numbers[n-1]
	swap smallest number with numbers[i]

pseudocode for a bubble sort
O(n^2)

reperat n times
	for i from 0 to n-2
		if numbers[i] and numbers[i+1] out of order
			swap them
	if no swaps
	quit

Recursion:

It's a function that call's itself

#include <stdio.h>
#include <cs50.h>

//declare function prototype
void draw(int n);

int main(void)
{
    int h = get_int("Height: ");
    draw(h) ;
}

/*
Recursion implementation
*/
void draw(int n)
{
    //Base case
    if(n <= 0)
    {
        return;
    }
    draw(n-1);
    for (int i = 0; i < n; i++)
    {
        printf("#");
    }
    printf("\n");
}

Similar to: Attachments/Pasted image 20260614210705.png


Merge sort:

Faster than selection sort and bubble sort. Psuedocode:

if only one number
	quit
else
	sort left half of the numbers
	sort right half of the numbers
	merge the sorted halves

step 1:sort the left half
[6,3,4,1,5,2,7,0]
Attachments/Pasted image 20260615184013.png
step merge them:
Attachments/Pasted image 20260615184522.png

Powered by Forestry.md