finish lab25

This commit is contained in:
Kaehvaman 2024-11-30 22:22:40 +04:00
parent dc58ebe57f
commit 92f7e73b20
4 changed files with 155 additions and 53 deletions

View File

@ -71,6 +71,13 @@ int ArraysAreEqual() {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
if (counts_in_words[i] != counts_in_a[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; return 0;
} }
} }
@ -160,7 +167,7 @@ void BubbleSortStrings() {
void InsertionSortStrings() { void InsertionSortStrings() {
int i = 1; int i = 1;
while (i < MAX_WORDS) { while (i < n) {
int j = i; int j = i;
while (j > 0 && strcmp(a[j - 1], a[j]) > 0) { while (j > 0 && strcmp(a[j - 1], a[j]) > 0) {
swapWords(a[j], a[j - 1]); swapWords(a[j], a[j - 1]);
@ -171,75 +178,75 @@ void InsertionSortStrings() {
} }
// Merges two subarrays of arr[]. // Merges two subarrays of arr[].
// First subarray is arr[l..m] // First subarray is arr[left..mid]
// Second subarray is arr[m+1..r] // Second subarray is arr[mid+1..right]
void merge(char arr[MAX_WORDS][MAX_LEN_WORD], int l, int m, int r) void merge(char arr[MAX_WORDS][MAX_LEN_WORD], int left, int mid, int right) {
{
int i, j, k; int i, j, k;
int n1 = m - l + 1; int n1 = mid - left + 1;
int n2 = r - m; int n2 = right - mid;
// Create temp arrays // Create temporary arrays
char L[MAX_WORDS][MAX_LEN_WORD], R[MAX_WORDS][MAX_LEN_WORD]; 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++) { for (i = 0; i < n1; i++) {
strcpy(L[i], arr[l + i]); strcpy(leftArr[i], arr[left + i]);
} }
for (j = 0; j < n2; j++) { 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; i = 0;
j = 0; j = 0;
k = l; k = left;
while (i < n1 && j < n2) { while (i < n1 && j < n2) {
if (L[i] <= R[j]) { if (strcmp(leftArr[i], rightArr[j]) <= 0) {
strcpy(arr[k], L[i]); strcpy(arr[k], leftArr[i]);
i++; i++;
} }
else { else {
strcpy(arr[k], R[j]); strcpy(arr[k], rightArr[j]);
j++; j++;
} }
k++; k++;
} }
// Copy the remaining elements of L[], // Copy the remaining elements of leftArr[], if any
// if there are any
while (i < n1) { while (i < n1) {
strcpy(arr[k], L[i]); strcpy(arr[k], leftArr[i]);
i++; i++;
k++; k++;
} }
// Copy the remaining elements of R[], // Copy the remaining elements of rightArr[], if any
// if there are any
while (j < n2) { while (j < n2) {
strcpy(arr[k], R[j]); strcpy(arr[k], rightArr[j]);
j++; j++;
k++; k++;
} }
} }
// l is for left index and r is right index of the // The subarray to be sorted is in the index range [left-right]
// sub-array of arr to be sorted void MergeSortStrings(char arr[MAX_WORDS][MAX_LEN_WORD], int left, int right) {
void MergeSortStrings(char arr[MAX_WORDS][MAX_LEN_WORD], int l, int r){ if (left < right) {
if (l < r) {
int m = l + (r - l) / 2; // Calculate the midpoint
int mid = left + (right - left) / 2;
// Sort first and second halves // Sort first and second halves
MergeSortStrings(arr, l, m); MergeSortStrings(arr, left, mid);
MergeSortStrings(arr, m + 1, r); MergeSortStrings(arr, mid + 1, right);
merge(arr, l, m, r); // Merge the sorted halves
merge(arr, left, mid, right);
} }
} }
void ShellSort() { void ShellSort() {
for (int s = MAX_WORDS / 2; s > 0; s /= 2) { for (int s = n / 2; s > 0; s /= 2) {
for (int i = s; i < MAX_WORDS; ++i) { for (int i = s; i < n; ++i) {
for (int j = i - s; j >= 0 && strcmp(a[j], a[j + s]) > 0; j -= s) { for (int j = i - s; j >= 0 && strcmp(a[j], a[j + s]) > 0; j -= s) {
char tmp[MAX_LEN_WORD]; char tmp[MAX_LEN_WORD];
strcpy(tmp, a[j]); strcpy(tmp, a[j]);

View File

@ -15,8 +15,6 @@ extern char words[MAX_WORDS][MAX_LEN_WORD];
// Ìàññèâ äëÿ ñîðòèðîâêè // Ìàññèâ äëÿ ñîðòèðîâêè
extern char a[MAX_WORDS][MAX_LEN_WORD]; extern char a[MAX_WORDS][MAX_LEN_WORD];
int LoadWords(char* filename); int LoadWords(char* filename);
void CopyWordsToA(); void CopyWordsToA();
int ArraysAreEqual(); int ArraysAreEqual();
@ -27,8 +25,8 @@ void SelectionSortStrings();
void QuickSortStrings(); void QuickSortStrings();
void BubbleSortStrings(); void BubbleSortStrings();
void InsertionSortStrings(); void InsertionSortStrings();
void MergeSortStrings(char arr[MAX_WORDS][MAX_LEN_WORD], int l, int r);
void ShellSort(); 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 LinearSearchStrings(char target[], char source[MAX_WORDS][MAX_LEN_WORD]);
int BinarySearchStrings(char target[], char source[MAX_WORDS][MAX_LEN_WORD]); int BinarySearchStrings(char target[], char source[MAX_WORDS][MAX_LEN_WORD]);

View File

@ -4,29 +4,126 @@
#include <time.h> #include <time.h>
#include "SortStringArray.h" #include "SortStringArray.h"
int main() { void printStrings(char arr[MAX_WORDS][MAX_LEN_WORD]) {
LoadWords("TextMarkup/dict4.txt"); 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;
clock_t start = clock(); char sort_funcs[][MAX_LEN_WORD] = { "SelectionSortStrings", "BubbleSortStrings", "InsertionSortStrings", "QuickSortStrings", "MergeSortStrings", "ShellSort" };
for (int i = 0; i < iter; i++) {
CopyWordsToA();
//memcpy(a, words, sizeof(words));
//SelectionSortStrings();
//QuickSortStrings();
MergeSortStrings(a, 0, MAX_WORDS-1);
void check() {
if (!ArraysAreEqual()) { if (!ArraysAreEqual()) {
printf("Arrays are not equal!!!\n"); printf("Arrays are not equal!!!\n");
} }
if (!isSortedStringArray(a)) { if (!isSortedStringArray(a)) {
printf("Array is not sorted!!!\n"); printf("Array is not sorted!!!\n");
} }
printStrings(words);
printStrings(a);
} }
void selsort() {
clock_t start = clock();
for (int i = 0; i < iter; i++) {
CopyWordsToA();
SelectionSortStrings();
//check();
}
printf("%s\n", sort_funcs[0]);
double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC; double runtime = (double)(clock() - start) / (double)CLOCKS_PER_SEC;
printf_s("full time = %.3lf seconds\n", runtime); printf_s("full time = %.10lf seconds\n", runtime);
printf_s("single sort time = %.3lf seconds\n", runtime / iter); 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; return 0;
} }

Binary file not shown.