mirror of
https://github.com/Kaehvaman/OAIP.git
synced 2025-01-18 16:49:11 +04:00
finish lab25
This commit is contained in:
parent
dc58ebe57f
commit
92f7e73b20
@ -71,6 +71,13 @@ int ArraysAreEqual() {
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (counts_in_words[i] != counts_in_a[i]) {
|
||||
printf("-----------\n");
|
||||
for (int i = 0; i < MAX_WORDS; i++) {
|
||||
if (1 || a[i][0] != '\0') {
|
||||
printf("%d) %s\n", counts_in_a[i], a[i]);
|
||||
}
|
||||
}
|
||||
printf("-----------\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -160,7 +167,7 @@ void BubbleSortStrings() {
|
||||
|
||||
void InsertionSortStrings() {
|
||||
int i = 1;
|
||||
while (i < MAX_WORDS) {
|
||||
while (i < n) {
|
||||
int j = i;
|
||||
while (j > 0 && strcmp(a[j - 1], a[j]) > 0) {
|
||||
swapWords(a[j], a[j - 1]);
|
||||
@ -171,75 +178,75 @@ void InsertionSortStrings() {
|
||||
}
|
||||
|
||||
// Merges two subarrays of arr[].
|
||||
// First subarray is arr[l..m]
|
||||
// Second subarray is arr[m+1..r]
|
||||
void merge(char arr[MAX_WORDS][MAX_LEN_WORD], int l, int m, int r)
|
||||
{
|
||||
// First subarray is arr[left..mid]
|
||||
// Second subarray is arr[mid+1..right]
|
||||
void merge(char arr[MAX_WORDS][MAX_LEN_WORD], int left, int mid, int right) {
|
||||
int i, j, k;
|
||||
int n1 = m - l + 1;
|
||||
int n2 = r - m;
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temp arrays
|
||||
char L[MAX_WORDS][MAX_LEN_WORD], R[MAX_WORDS][MAX_LEN_WORD];
|
||||
// Create temporary arrays
|
||||
static char leftArr[MAX_WORDS][MAX_LEN_WORD];
|
||||
static char rightArr[MAX_WORDS][MAX_LEN_WORD];
|
||||
|
||||
// Copy data to temp arrays L[] and R[]
|
||||
// Copy data to temporary arrays
|
||||
for (i = 0; i < n1; i++) {
|
||||
strcpy(L[i], arr[l + i]);
|
||||
strcpy(leftArr[i], arr[left + i]);
|
||||
}
|
||||
for (j = 0; j < n2; j++) {
|
||||
strcpy(R[i], arr[m + 1 + j]);
|
||||
strcpy(rightArr[j], arr[mid + 1 + j]);
|
||||
}
|
||||
|
||||
// Merge the temp arrays back into arr[l..r
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
i = 0;
|
||||
j = 0;
|
||||
k = l;
|
||||
k = left;
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
strcpy(arr[k], L[i]);
|
||||
if (strcmp(leftArr[i], rightArr[j]) <= 0) {
|
||||
strcpy(arr[k], leftArr[i]);
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
strcpy(arr[k], R[j]);
|
||||
strcpy(arr[k], rightArr[j]);
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[],
|
||||
// if there are any
|
||||
// Copy the remaining elements of leftArr[], if any
|
||||
while (i < n1) {
|
||||
strcpy(arr[k], L[i]);
|
||||
strcpy(arr[k], leftArr[i]);
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[],
|
||||
// if there are any
|
||||
// Copy the remaining elements of rightArr[], if any
|
||||
while (j < n2) {
|
||||
strcpy(arr[k], R[j]);
|
||||
strcpy(arr[k], rightArr[j]);
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
// l is for left index and r is right index of the
|
||||
// sub-array of arr to be sorted
|
||||
void MergeSortStrings(char arr[MAX_WORDS][MAX_LEN_WORD], int l, int r){
|
||||
if (l < r) {
|
||||
int m = l + (r - l) / 2;
|
||||
// The subarray to be sorted is in the index range [left-right]
|
||||
void MergeSortStrings(char arr[MAX_WORDS][MAX_LEN_WORD], int left, int right) {
|
||||
if (left < right) {
|
||||
|
||||
// Calculate the midpoint
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
MergeSortStrings(arr, l, m);
|
||||
MergeSortStrings(arr, m + 1, r);
|
||||
MergeSortStrings(arr, left, mid);
|
||||
MergeSortStrings(arr, mid + 1, right);
|
||||
|
||||
merge(arr, l, m, r);
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
void ShellSort() {
|
||||
for (int s = MAX_WORDS / 2; s > 0; s /= 2) {
|
||||
for (int i = s; i < MAX_WORDS; ++i) {
|
||||
for (int s = n / 2; s > 0; s /= 2) {
|
||||
for (int i = s; i < n; ++i) {
|
||||
for (int j = i - s; j >= 0 && strcmp(a[j], a[j + s]) > 0; j -= s) {
|
||||
char tmp[MAX_LEN_WORD];
|
||||
strcpy(tmp, a[j]);
|
||||
|
@ -15,8 +15,6 @@ extern char words[MAX_WORDS][MAX_LEN_WORD];
|
||||
// Ìàññèâ äëÿ ñîðòèðîâêè
|
||||
extern char a[MAX_WORDS][MAX_LEN_WORD];
|
||||
|
||||
|
||||
|
||||
int LoadWords(char* filename);
|
||||
void CopyWordsToA();
|
||||
int ArraysAreEqual();
|
||||
@ -27,8 +25,8 @@ void SelectionSortStrings();
|
||||
void QuickSortStrings();
|
||||
void BubbleSortStrings();
|
||||
void InsertionSortStrings();
|
||||
void MergeSortStrings(char arr[MAX_WORDS][MAX_LEN_WORD], int l, int r);
|
||||
void ShellSort();
|
||||
void MergeSortStrings(char arr[MAX_WORDS][MAX_LEN_WORD], int left, int right);
|
||||
|
||||
int LinearSearchStrings(char target[], char source[MAX_WORDS][MAX_LEN_WORD]);
|
||||
int BinarySearchStrings(char target[], char source[MAX_WORDS][MAX_LEN_WORD]);
|
||||
|
@ -4,29 +4,126 @@
|
||||
#include <time.h>
|
||||
#include "SortStringArray.h"
|
||||
|
||||
int main() {
|
||||
LoadWords("TextMarkup/dict4.txt");
|
||||
void printStrings(char arr[MAX_WORDS][MAX_LEN_WORD]) {
|
||||
printf("-----------\n");
|
||||
for (int i = 0; i < MAX_WORDS; i++) {
|
||||
if (1 || arr[i][0] != '\0') {
|
||||
printf("%s\n", arr[i]);
|
||||
}
|
||||
}
|
||||
printf("-----------\n");
|
||||
}
|
||||
|
||||
int iter = 3;
|
||||
int iter = 10;
|
||||
char sort_funcs[][MAX_LEN_WORD] = { "SelectionSortStrings", "BubbleSortStrings", "InsertionSortStrings", "QuickSortStrings", "MergeSortStrings", "ShellSort" };
|
||||
|
||||
void check() {
|
||||
if (!ArraysAreEqual()) {
|
||||
printf("Arrays are not equal!!!\n");
|
||||
}
|
||||
if (!isSortedStringArray(a)) {
|
||||
printf("Array is not sorted!!!\n");
|
||||
}
|
||||
printStrings(words);
|
||||
printStrings(a);
|
||||
}
|
||||
|
||||
void selsort() {
|
||||
clock_t start = clock();
|
||||
for (int i = 0; i < iter; i++) {
|
||||
CopyWordsToA();
|
||||
//memcpy(a, words, sizeof(words));
|
||||
|
||||
//SelectionSortStrings();
|
||||
//QuickSortStrings();
|
||||
MergeSortStrings(a, 0, MAX_WORDS-1);
|
||||
|
||||
if (!ArraysAreEqual()) {
|
||||
printf("Arrays are not equal!!!\n");
|
||||
}
|
||||
if (!isSortedStringArray(a)) {
|
||||
printf("Array is not sorted!!!\n");
|
||||
}
|
||||
SelectionSortStrings();
|
||||
//check();
|
||||
}
|
||||
printf("%s\n", sort_funcs[0]);
|
||||
double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC;
|
||||
printf_s("full time = %.3lf seconds\n", runtime);
|
||||
printf_s("single sort time = %.3lf seconds\n", runtime / iter);
|
||||
printf_s("full time = %.10lf seconds\n", runtime);
|
||||
printf_s("single sort time = %.10lf seconds\n", runtime / (double)iter);
|
||||
}
|
||||
|
||||
void bubsort() {
|
||||
clock_t start = clock();
|
||||
for (int i = 0; i < iter; i++) {
|
||||
CopyWordsToA();
|
||||
|
||||
BubbleSortStrings();
|
||||
|
||||
//check();
|
||||
}
|
||||
printf("%s\n", sort_funcs[1]);
|
||||
double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC;
|
||||
printf_s("full time = %.10lf seconds\n", runtime);
|
||||
printf_s("single sort time = %.10lf seconds\n", runtime / (double)iter);
|
||||
}
|
||||
|
||||
void inssert() {
|
||||
clock_t start = clock();
|
||||
for (int i = 0; i < iter; i++) {
|
||||
CopyWordsToA();
|
||||
InsertionSortStrings();
|
||||
//check();
|
||||
}
|
||||
printf("%s\n", sort_funcs[2]);
|
||||
double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC;
|
||||
printf_s("full time = %.10lf seconds\n", runtime);
|
||||
printf_s("single sort time = %.10lf seconds\n", runtime / (double)iter);
|
||||
}
|
||||
|
||||
void quicksort() {
|
||||
clock_t start = clock();
|
||||
for (int i = 0; i < iter; i++) {
|
||||
CopyWordsToA();
|
||||
QuickSortStrings();
|
||||
|
||||
//check();
|
||||
}
|
||||
printf("%s\n", sort_funcs[3]);
|
||||
double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC;
|
||||
printf_s("full time = %.10lf seconds\n", runtime);
|
||||
printf_s("single sort time = %.10lf seconds\n", runtime / (double)iter);
|
||||
}
|
||||
|
||||
void mergesort() {
|
||||
clock_t start = clock();
|
||||
for (int i = 0; i < iter; i++) {
|
||||
CopyWordsToA();
|
||||
MergeSortStrings(a, 0, n - 1);
|
||||
|
||||
//check();
|
||||
}
|
||||
printf("%s\n", sort_funcs[4]);
|
||||
double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC;
|
||||
printf_s("full time = %.10lf seconds\n", runtime);
|
||||
printf_s("single sort time = %.10lf seconds\n", runtime / (double)iter);
|
||||
}
|
||||
|
||||
void shellsort() {
|
||||
clock_t start = clock();
|
||||
for (int i = 0; i < iter; i++) {
|
||||
CopyWordsToA();
|
||||
|
||||
ShellSort();
|
||||
//check();
|
||||
}
|
||||
printf("%s\n", sort_funcs[5]);
|
||||
double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC;
|
||||
printf_s("full time = %.10lf seconds\n", runtime);
|
||||
printf_s("single sort time = %.10lf seconds\n", runtime / (double)iter);
|
||||
}
|
||||
|
||||
int main() {
|
||||
char filename[] = "TextMarkup/dict4b.txt";
|
||||
LoadWords(filename);
|
||||
printf("%s\n\n", filename);
|
||||
|
||||
selsort();
|
||||
bubsort();
|
||||
inssert();
|
||||
quicksort();
|
||||
mergesort();
|
||||
shellsort();
|
||||
|
||||
printf("\a");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user