Insertion

.c
School
Northeastern University**We aren't endorsed by this school
Course
CS 5008
Subject
Computer Science
Date
Jan 15, 2025
Pages
3
Uploaded by JudgeOkapiMaster1201
// Compile with: clang -g insertion.c -o insertion// Run with: ./insertion// your implementation must use the provided swap function#include <stdio.h> // Include file for standard input/output#include <stdlib.h> // so we can use atoi()#include <time.h> // so we can use time_t and clock_gettime()#ifndef EXPERIMENT#define EXPERIMENT 0 // we will use this for Part 4 of this lab#endif// =============== Helper Functions ===============// Swaps two numbers in an array// Input: The 'address of' two elements in an array that must be swapped.void swap(int* a, int* b) {// TODO: Swap two integers in an array.int temp = *a;*a = *b;*b = temp;}// Input: A pointer to an array (i.e. the array itself points to the first index)// The size of the array (Because we do not know how big the array is automatically)void printIntArray(int* array, unsigned int size) {unsigned int i; // Note: 'unsigned int' is a datatype for storing positive integers.for (i = 0; i < size; i++) {printf("%d ", array[i]);}printf("\n");}// =============== Sort Function ===============// Provided below is a sort function. I have also// provided a template for how to document functions// to help organize your code.// Name: sort// Input(s):// - 'array' is a pointer to an integer address.// This is the start of some 'contiguous block of memory' that we will sort.// - 'size' tells us how big the array of data is we are sorting.// Output: No value is returned, but 'array' should be modified to store a sorted array of numbers.void sortIntegers(int* array, unsigned int size) {int i, j;for (i = 1; i < size; i++) {int key = array[i];j = i - 1;// Move elements of array[0..i-1] that are greater than key to one position ahead of their current positionwhile (j >= 0 && array[j] > key) {swap(&array[j + 1], &array[j]);j = j - 1;}array[j + 1] = key;}
Background image
}// =============== Main Functions ===============int main(int argc, char* argv[]) {#if EXPERIMENT == 0// Some test data sets.int dataset1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int dataset2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};int dataset3[] = {0, 3, 2, 1, 4, 7, 6, 5, 8, 9, 10};int dataset4[] = {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};int dataset5[] = {100, 201, 52, 3223, 24, 55, 623, 75, 8523, -9, 150};int dataset6[] = {-1, 1, 2, -3, 4, 5, -6, 7, 8, -9, 10};// Sort our integer arraysortIntegers(dataset1, 11);sortIntegers(dataset2, 11);sortIntegers(dataset3, 11);sortIntegers(dataset4, 11);sortIntegers(dataset5, 11);sortIntegers(dataset6, 11);// Print out an arrayprintIntArray(dataset1, 11);printIntArray(dataset2, 11);printIntArray(dataset3, 11);printIntArray(dataset4, 11);printIntArray(dataset5, 11);printIntArray(dataset6, 11);#elseif (argc != 2) {printf("One argument expected: ./insertion 1000\n");return 1;}// Convert the argument of the program into an integerconst int size = atoi(argv[1]);// Generate a random seedtime_t t;srand((unsigned)time(&t));// Allocate memoryint* random = (int*)malloc(sizeof(int) * size);// Populate our test data setfor (int i = 0; i < size; i++) {// Generate random values from 0 to 99random[i] = rand() % size;}// You can uncomment if you'd like to see the size sorted// printf("Before the sort: ");// printIntArray(random, size);// Setup timersstruct timespec begin, end;// Get the time before we startclock_gettime(CLOCK_MONOTONIC_RAW, &begin);
Background image
// Perform the sortsortIntegers(random, size);// Get the time after we are doneclock_gettime(CLOCK_MONOTONIC_RAW, &end);double time_taken = (end.tv_nsec - begin.tv_nsec) / 1000000000.0 + (end.tv_sec - begin.tv_sec);printf("Total time = %f seconds\n", time_taken);// Confirm the sort worked// printf("After the sort: ");// printIntArray(random, size);// Free our random arrayfree(random);#endifreturn 0;}
Background image