Holberton logo

0x1A. C - Sorting algorithms & Big O

sort header

Table of Contents


Project Details

Synopsis

This project introduces concepts of data structures and algorithms in programming. It is meant to be done by groups of two students. Each group of two should pair program for at least the mandatory part.

What students should learn from this project

At the end of this project students are expected to be able to explain to anyone, without the help of Google:

  • At least four different sorting algorithms
  • What is the Big O notation, and how to evaluate the time complexity of an algorithm
  • How to select the best sorting algorithm for a given input
  • What is a stable sorting algorithm

Resources

Project Requirements

  • Allowed editors: vi, vim, emacs
  • All your files will be compiled on Ubuntu 14.04 LTS
  • Your programs and functions will be compiled with gcc 4.8.4 (C90) using the flags -Wall -Werror -Wextra and -pedantic

image

  • All your files should end with a new line
  • A README.md file, at the root of the folder of the project is mandatory
  • Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl
  • You are not allowed to use global variables
  • No more than 5 functions per file
  • Unless specified otherwise, you are not allowed to use the standard library. Any use of functions like printf, puts, ... is totally forbidden.
  • In the following examples, the main.c files are showed as examples. You can use them to test your functions, but you don't have to push them to your repo (if you do we won't take them into account). We will use our own main.c files at compilation. Our main.c files might be different from the one showed in the examples
  • The prototypes of all your functions should be included in your header file called sort.h
  • Don't forget to push your header file
  • All your header files should be include guarded
  • A list/array does not need to be sorted if its size is less than 2.
  • For this project you are given the following print_array, and print_list functions:
#include <stdlib.h>
#include <stdio.h>

/**
 * print_array - Prints an array of integers
 *
 * @array: The array to be printed
 * @size: Number of elements in @array
 */

void print_array(const int *array, size_t size)
{
	size_t i;

	i = 0;
	while (array && i < size)
	{
		if (i > 0)
			printf(", ");
		printf("%d", array[i]);
		++i;
	}
	printf("\n");
}
#include <stdio.h>
#include "sort.h"

/**
 * print_list - Prints a list of integers
 *
 * @list: The list to be printed
 */

void print_list(const listint_t *list)
{
	int i;

	i = 0;
	while (list)
	{
		if (i > 0)
			printf(", ");
		printf("%d", list->n);
		++i;
		list = list->next;
	}
	printf("\n");
}
  • Our files print_array.c and print_list.c (containing the print_array and print_list functions) will be compiled with your functions during the correction.
  • Please declare the prototype of the functions print_array and print_list in your sort.h header file
  • Please use the following data structure for doubly linked list:
/**
 * struct listint_s - Doubly linked list node
 *
 * @n: Integer stored in the node
 * @next: Pointer to the next element of the list
 */

typedef struct listint_s
{
	const int n;
	struct listint_s *prev;
	struct listint_s *next;
} listint_t;

For almost all sorting algorithms you will have to work on, you will be asked to write a file containing the big O notation of the time complexity of the algorithm. Please use this format:

  • O(1)
  • O(n)
  • O(n!)
  • n square -> O(n^2)
  • log(n) -> O(log(n))
  • n * log(n) -> O(nlog(n))
  • ...
  • Please use the "short" notation (don't use constants). Example: O(nk) or O(wn) should be written O(n). All your answers files must have an empty line at the end.

Project

Task list

Task # Type File name and link Short description
0 Mandatory Write a function that sorts an array of integers in ascending order using the Bubble sort algorithm

Write a function that sorts an array of integers in ascending order using the Bubble sort algorithm

  • Prototype: void bubble_sort(int *array, size_t size);
  • You're expected to print the array after each time you swap two elements (See example below)
  • Write in the file 0-O, the big O notations of the time complexity of the Bubble sort algorithm, with 1 notation per line:

  • in the best case
  • in the average case
  • in the worst case
  • 1 Mandatory Write a function that sorts a doubly linked list of integers in ascending order using the Insertion sort algorithm

  • Prototype: void insertion_sort_list(listint_t **list);
  • You are not allowed to modify the integer n of a node. You have to swap the nodes themselves.
  • You're expected to print the list after each time you swap two elements (See example below)
  • Write in the file 1-O, the big O notations of the time complexity of the Insertion sort algorithm, with 1 notation per line:

  • in the best case
  • in the average case
  • in the worst case
  • 2 Mandatory Write a function that sorts an array of integers in ascending order using the Selection sort algorithm

  • Prototype: void selection_sort(int *array, size_t size);
  • You're expected to print the array after each time you swap two elements (See example below)
  • Write in the file 2-O, the big O notations of the time complexity of the Selection sort algorithm, with 1 notation per line:

  • in the best case
  • in the average case
  • in the worst case
  • 3 Mandatory Write a function that sorts an array of integers in ascending order using the Quick sort algorithm

  • Prototype: void quick_sort(int *array, size_t size);
  • You must implement the Lomuto partition scheme.
  • The pivot should always be the last element of the partition being sorted.
  • You're expected to print the array after each time you swap two elements (See example below)
  • Write in the file 3-O, the big O notations of the time complexity of the Quick sort algorithm, with 1 notation per line:

  • in the best case
  • in the average case
  • in the worst case
  • 4 Mandatory What is the time complexity of this function / algorithm?
    void f(int n)
    {
    int i;
    
    for (i = 0; i &lt; n; i++)
    {
        printf(&quot;[%d]\n&quot;, i);
    }
    

    } | 5 | Mandatory | |What is the time complexity of this function / algorithm? | 6 | Mandatory | |What is the time complexity of this function / algorithm? | 7 | Mandatory | |What is the time complexity of this function / algorithm? | 8 | Mandatory | |What is the time complexity of this function / algorithm? | 9 | Mandatory | |What is the time complexity of this function / algorithm? | 10 | Mandatory | |What is the time complexity of this function / algorithm? | 11 | Mandatory | |What is the time complexity of this function / algorithm? | 12 | Mandatory | |What is the time complexity of this function / algorithm? | 13 | Mandatory | |What is the time complexity of this function / algorithm? | 14 | Mandatory | |What is the time complexity of this function / algorithm? | 15 | Mandatory | |

    What are the time complexities of those operations on an unsorted array (one answer per line):

  • Accessing the nth element
  • Inserting at index n
  • Removing at index n
  • Searching for an element in an array of size n
  • Setting value at index n
  • | 16 | Mandatory | |

    What are the time complexities of those operations on a singly linked list (one answer per line):

  • Accessing the nth element
  • Inserting after the nth element (Considering you have a pointer to the node to insert)
  • Removing the nth element (Considering you have a pointer to the node to remove)
  • Searching for an element in a linked list of size n
  • Setting the value of the nth element (Considering you have a pointer to the node to set the value of)
  • | 17 | Mandatory | |

    What are the time complexities of those operations on a doubly linked list (one answer per line):

  • Accessing the nth element
  • Inserting after the nth element (Considering you have a pointer to the node to insert)
  • Removing the nth element (Considering you have a pointer to the node to remove)
  • Searching for an element in a linked list of size n
  • Setting the value of the nth element (Considering you have a pointer to the node to set the value of)
  • | 18 | Mandatory | |

    What are the time complexities of those operations on an unsorted Python 3 list (one answer per line):

  • Accessing the nth element
  • Inserting at index n
  • Removing at index n
  • Searching for an element in a Python list of size n
  • Setting value at index n
  • | 19 | Mandatory | |

    What are the time complexities of those operations on a stack (one answer per line):

  • push
  • pop
  • Searching for an element in a stack of size n
  • | 20 | Mandatory | |

    What are the time complexities of those operations on a queue (one answer per line):

  • push
  • pop
  • Searching for an element in a queue of size n
  • | 21 | Mandatory | |

    What are the time complexities of those operations on a hash table (one answer per line) - with the implementation you used during the previous Hash Table C project (chaining):

  • Searching for an element, best case
  • Searching for an element, worst case
  • Insertion, best case
  • Insertion, worst case
  • Deletion, best case
  • Deletion, worst case
  • | 21 | Mandatory | | | 22 | Advanced| | |

    Status

    Stories in Ready Throughput Graph

    Team

    Carrie Ybay Elaine Yeung
    Carrie Ybay Elaine Yeung

    Credits