create assignment03
This commit is contained in:
4 files changed
+252
No files matched your search
@@ -0,0 +1,141 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#define NO_LEN 0
|
||||||
|
#define MIN_SIZE 2
|
||||||
|
#define INDEX_OFFSET 1
|
||||||
|
#define INSERTION_SORT 1
|
||||||
|
#define SELECTION_SORT 2
|
||||||
|
|
||||||
|
void insertionSort(int *arr, size_t size)
|
||||||
|
{
|
||||||
|
if (size < MIN_SIZE)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int key;
|
||||||
|
int cmp;
|
||||||
|
|
||||||
|
for (int i = 0; i < size - INDEX_OFFSET; i++)
|
||||||
|
{
|
||||||
|
key = i + INDEX_OFFSET;
|
||||||
|
cmp = i;
|
||||||
|
|
||||||
|
while (arr[key] < arr[cmp])
|
||||||
|
{
|
||||||
|
int tmp;
|
||||||
|
|
||||||
|
tmp = arr[key];
|
||||||
|
arr[key] = arr[cmp];
|
||||||
|
arr[cmp] = tmp;
|
||||||
|
|
||||||
|
key--;
|
||||||
|
cmp--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void selectionSort(int *arr, size_t size)
|
||||||
|
{
|
||||||
|
if (size < MIN_SIZE)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
int min;
|
||||||
|
int tmp;
|
||||||
|
|
||||||
|
min = i;
|
||||||
|
|
||||||
|
for (int j = i + INDEX_OFFSET; j < size; j++)
|
||||||
|
{
|
||||||
|
if (arr[j] < arr[min])
|
||||||
|
{
|
||||||
|
min = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = arr[i];
|
||||||
|
arr[i] = arr[min];
|
||||||
|
arr[min] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printArr(int *arr, size_t size)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
char *padding;
|
||||||
|
padding = i < size - INDEX_OFFSET ? ", " : "\n";
|
||||||
|
printf("%d%s", arr[i], padding);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
size_t arrSize;
|
||||||
|
int *arr;
|
||||||
|
|
||||||
|
printf("Enter Number of elements: ");
|
||||||
|
scanf("%ld", &arrSize);
|
||||||
|
|
||||||
|
arr = (int*) malloc(sizeof(int) * arrSize);
|
||||||
|
|
||||||
|
for (int i = 0; i < arrSize; i++)
|
||||||
|
{
|
||||||
|
printf("Element %d: ", i + INDEX_OFFSET);
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int sortAlgorithm;
|
||||||
|
int validAlgorithm;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
printf("\nChoose a sorting algorithm:\n");
|
||||||
|
printf("Insertion Sort (1)\n");
|
||||||
|
printf("Selection Sort (2)\n");
|
||||||
|
|
||||||
|
scanf("%d", &sortAlgorithm);
|
||||||
|
|
||||||
|
validAlgorithm = sortAlgorithm == INSERTION_SORT ||
|
||||||
|
sortAlgorithm == SELECTION_SORT;
|
||||||
|
|
||||||
|
if (!validAlgorithm)
|
||||||
|
{
|
||||||
|
printf("Invalid sort algorithm\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (!validAlgorithm);
|
||||||
|
|
||||||
|
printf("\nBefore:\n");
|
||||||
|
printArr(arr, arrSize);
|
||||||
|
|
||||||
|
char *algorithm;
|
||||||
|
|
||||||
|
switch (sortAlgorithm)
|
||||||
|
{
|
||||||
|
case INSERTION_SORT:
|
||||||
|
algorithm = "insertion";
|
||||||
|
insertionSort(arr, arrSize);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SELECTION_SORT:
|
||||||
|
algorithm = "selection";
|
||||||
|
selectionSort(arr, arrSize);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Error: bad input");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nAfter %s sort:\n", algorithm);
|
||||||
|
printArr(arr, arrSize);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,111 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define NO_SIZE 0
|
||||||
|
#define INTEGER 1
|
||||||
|
#define FLOAT 2
|
||||||
|
#define STRING 3
|
||||||
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
void swap(void *a, void *b, size_t size)
|
||||||
|
{
|
||||||
|
void *tmp;
|
||||||
|
|
||||||
|
tmp = malloc(size);
|
||||||
|
|
||||||
|
memcpy(tmp, a, size);
|
||||||
|
memcpy(a, b, size);
|
||||||
|
memcpy(b, tmp, size);
|
||||||
|
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printValue(char *message, void *data, int type)
|
||||||
|
{
|
||||||
|
printf("%s ", message);
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case INTEGER:
|
||||||
|
printf("%d\n", *((int*) data));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FLOAT:
|
||||||
|
printf("%.1f\n", *((float *) data));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STRING:
|
||||||
|
printf("%s\n", (char *) data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int type;
|
||||||
|
int validType;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
printf("Choose a data type:\n");
|
||||||
|
printf("Integer (1)\n");
|
||||||
|
printf("Float (2)\n");
|
||||||
|
printf("String (3)\n");
|
||||||
|
|
||||||
|
scanf("%d", &type);
|
||||||
|
|
||||||
|
validType = type >= INTEGER && type <= STRING;
|
||||||
|
|
||||||
|
if (!validType)
|
||||||
|
{
|
||||||
|
printf("Invalid Data Type\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (!validType);
|
||||||
|
|
||||||
|
char *format;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
format = type == INTEGER ? "%d" :
|
||||||
|
type == FLOAT ? "%f" :
|
||||||
|
type == STRING ? "%255s" :
|
||||||
|
NULL;
|
||||||
|
|
||||||
|
size = type == INTEGER ? sizeof(int) :
|
||||||
|
type == FLOAT ? sizeof(float) :
|
||||||
|
type == STRING ? sizeof(char) * BUFFER_SIZE :
|
||||||
|
0;
|
||||||
|
|
||||||
|
if (format == NULL || size == NO_SIZE)
|
||||||
|
{
|
||||||
|
printf("Invalid Data Type");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *a;
|
||||||
|
void *b;
|
||||||
|
|
||||||
|
a = malloc(size);
|
||||||
|
b = malloc(size);
|
||||||
|
|
||||||
|
printf("\nEnter value A: ");
|
||||||
|
scanf(format, a);
|
||||||
|
|
||||||
|
printf("Enter value B: ");
|
||||||
|
scanf(format, b);
|
||||||
|
|
||||||
|
printValue("\nA value before: ", a, type);
|
||||||
|
printValue("B value before: ", b, type);
|
||||||
|
|
||||||
|
printf("\nSwapping...\n\n");
|
||||||
|
swap(a, b, size);
|
||||||
|
|
||||||
|
printValue("A value after: ", a, type);
|
||||||
|
printValue("B value after: ", b, type);
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
free(b);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user