Upload batch 35/306
Browse files- שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 3.pdf +3 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 4.pdf +3 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 5.pdf +3 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 6.pdf +3 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 7.pdf +3 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 8.pdf +3 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 9.pdf +3 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-1.c +77 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-10.c +207 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-11.c +385 -0
שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 3.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a6577ba75d3d676f6a3f2fce14714e122d5ab94c6a8bbb602449da0e38b583e7
|
| 3 |
+
size 715252
|
שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 4.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6168fbbae680dc23a7902e064f9a368565afa237868a578e62691057bfc3c712
|
| 3 |
+
size 749893
|
שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 5.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1d52ad6c70765615dfc6e9af8fa3b7685834e9b84cf200591fce53162706e4fe
|
| 3 |
+
size 660153
|
שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 6.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:be34f9c68513b7e2663f493002f4c94de5cf81615e66a339b45be65a7a705939
|
| 3 |
+
size 993223
|
שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 7.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05ad08625d9b8d217d6cbf2eb5562602ecbd25358186a25d03ab26e0698ac8ed
|
| 3 |
+
size 755238
|
שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 8.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c36e1b766b1518e35b54fe02a4d2f1e71cfc6cb7ef6d4a1335a2663695ca2fee
|
| 3 |
+
size 717892
|
שנה א/מבוא למדעי המחשב/עבודות בית/מטלת בית מספר 9.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e10b56ff6876578136a28d7847ebc321ee953ef503625f5391c8bf36187e0af5
|
| 3 |
+
size 609178
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-1.c
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
|
| 3 |
+
// Function to calculate and print the birth year
|
| 4 |
+
void printDate() {
|
| 5 |
+
int currentYear = 2024;
|
| 6 |
+
int age = 26;
|
| 7 |
+
printf("Birth Year is: %d\n", currentYear - age);
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
// Function to print a nicely aligned pattern resembling 'X' in one printf statement
|
| 11 |
+
void printX() {
|
| 12 |
+
printf("* *\n"
|
| 13 |
+
" * * \n"
|
| 14 |
+
" * * \n"
|
| 15 |
+
" * \n"
|
| 16 |
+
" * * \n"
|
| 17 |
+
" * * \n"
|
| 18 |
+
"* *\n");
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// Function to calculate and print earnings compensation for a worker
|
| 22 |
+
void printBobEranings() {
|
| 23 |
+
long id = 306543365;
|
| 24 |
+
double hourlyRate = 50.3;
|
| 25 |
+
int hoursWorkedDay = 8;
|
| 26 |
+
int daysWorkedInWeek = 5;
|
| 27 |
+
int yearFired = 2024;
|
| 28 |
+
int yearHired = 1990;
|
| 29 |
+
|
| 30 |
+
int yearsWorked = yearFired - yearHired; // Calculate total years worked
|
| 31 |
+
double monthWorth = hourlyRate * hoursWorkedDay * daysWorkedInWeek * 4; // Approximate monthly earnings
|
| 32 |
+
double compensation = yearsWorked * monthWorth; // Total compensation based on years worked
|
| 33 |
+
|
| 34 |
+
printf("Congratulations Worker ID: %ld\n", id);
|
| 35 |
+
printf("Thank you for the %d years you've worked\n", yearsWorked);
|
| 36 |
+
printf("You are entitled to a sum of: %.2f\n", compensation);
|
| 37 |
+
printf("Thank you and Goodbye!\n");
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// Function to calculate and print average amount paid per item by Alice
|
| 41 |
+
void printAliceBill() {
|
| 42 |
+
double productA = 2.0;
|
| 43 |
+
double productB = 2.5;
|
| 44 |
+
int boughtA = 10;
|
| 45 |
+
int boughtB = 2;
|
| 46 |
+
|
| 47 |
+
double totalPaid = (productA * boughtA) + (productB * boughtB); // Total amount spent
|
| 48 |
+
double totalItems = boughtA + boughtB; // Total items bought
|
| 49 |
+
double average = totalPaid / totalItems; // Average amount per item
|
| 50 |
+
|
| 51 |
+
printf("Alice paid in average %f NIS per item\n", average);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// Function to calculate and print the estimated arrival time based on distance and speed
|
| 55 |
+
void printArrivelTime() {
|
| 56 |
+
double distance = 300; // Distance to travel in KM
|
| 57 |
+
double speed = 120; // Speed in KMH
|
| 58 |
+
|
| 59 |
+
double timeInHours = distance / speed; // Calculate total time in hours
|
| 60 |
+
int hours = timeInHours; // Extract whole hours
|
| 61 |
+
int minutes = (timeInHours - hours) * 60; // Calculate remaining minutes
|
| 62 |
+
|
| 63 |
+
printf("Arriving in %d hours and %d minutes.\n", hours, minutes);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
int main() {
|
| 67 |
+
printDate();
|
| 68 |
+
printf("--------------\n");
|
| 69 |
+
printX();
|
| 70 |
+
printf("--------------\n");
|
| 71 |
+
printBobEranings();
|
| 72 |
+
printf("--------------\n");
|
| 73 |
+
printAliceBill();
|
| 74 |
+
printf("--------------\n");
|
| 75 |
+
printArrivelTime();
|
| 76 |
+
return 0;
|
| 77 |
+
}
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
|
| 3 |
+
int findIndex(int* arr, int n) {
|
| 4 |
+
for (int i = 0; i < n; i++)
|
| 5 |
+
if (arr[i] == i)
|
| 6 |
+
return i;
|
| 7 |
+
return -1;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
int findIndexLog(int* arr, int n) {
|
| 11 |
+
int mid, low = 0, high = n - 1; // Initialize low and high for binary search
|
| 12 |
+
|
| 13 |
+
// Step 1: Check boundary cases
|
| 14 |
+
if (arr[low] == low) return low; // If the first element matches its index
|
| 15 |
+
if (arr[high] == high) return high; // If the last element matches its index
|
| 16 |
+
|
| 17 |
+
// Step 2: Perform binary search
|
| 18 |
+
while (low <= high) { // Continue while the search range is valid
|
| 19 |
+
mid = (low + high) / 2; // Calculate the middle index
|
| 20 |
+
|
| 21 |
+
if (arr[mid] == mid) { // If the middle element matches its index
|
| 22 |
+
return mid; // Return the matching index
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
if (arr[mid] > mid) { // If the middle value is greater than its index
|
| 26 |
+
high = mid - 1; // Search the left half
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
if (arr[mid] < mid) { // If the middle value is less than its index
|
| 30 |
+
low = mid + 1; // Search the right half
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// Step 3: If no match is found, return -1
|
| 35 |
+
return -1;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Iterative binary search for reverse-sorted array
|
| 39 |
+
int findKey(int* arr, int n, int key) {
|
| 40 |
+
int low = n - 1, high = 0, mid; // Initialize low and high for reverse-sorted array
|
| 41 |
+
|
| 42 |
+
// Step 1: Check boundary cases
|
| 43 |
+
if (arr[low] == key) return low; // Check if the key matches the last element
|
| 44 |
+
if (arr[high] == key) return high; // Check if the key matches the first element
|
| 45 |
+
|
| 46 |
+
// Step 2: Perform binary search
|
| 47 |
+
while (high <= low) { // Continue while the search range is valid
|
| 48 |
+
mid = (low + high) / 2; // Calculate the middle index
|
| 49 |
+
|
| 50 |
+
if (arr[mid] == key) return mid; // If the middle element matches the key, return its index
|
| 51 |
+
|
| 52 |
+
if (arr[mid] > key) { // If the middle value is greater than the key
|
| 53 |
+
high = mid + 1; // Search the right side
|
| 54 |
+
}
|
| 55 |
+
else if (arr[mid] < key) { // If the middle value is less than the key
|
| 56 |
+
low = mid - 1; // Search the left side
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
return -1; // If the key is not found, return -1
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// Recursive binary search for reverse-sorted array
|
| 64 |
+
int findKeyRe(int* arr, int n, int key, int low, int high) {
|
| 65 |
+
// Base Case: If the search range becomes invalid
|
| 66 |
+
if (low < high) return -1;
|
| 67 |
+
|
| 68 |
+
int mid = (low + high) / 2; // Calculate the middle index
|
| 69 |
+
|
| 70 |
+
if (arr[mid] == key) return mid; // If the middle element matches the key, return its index
|
| 71 |
+
|
| 72 |
+
if (arr[mid] > key) { // If the middle value is greater than the key
|
| 73 |
+
// Recursively search the right half (adjust high)
|
| 74 |
+
return findKeyRe(arr, n, key, low, mid + 1);
|
| 75 |
+
}
|
| 76 |
+
else if (arr[mid] < key) { // If the middle value is less than the key
|
| 77 |
+
// Recursively search the left half (adjust low)
|
| 78 |
+
return findKeyRe(arr, n, key, mid - 1, high);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
return -1; // This point should never be reached if all cases are handled
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// Wrapper function to initialize the recursion
|
| 85 |
+
int findKeyR(int* arr, int n, int key) {
|
| 86 |
+
// Call the recursive helper function with initial range for reverse-sorted array
|
| 87 |
+
return findKeyRe(arr, n, key, n - 1, 0);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
int findKeyAsc(int* arr, int n, int key) {
|
| 91 |
+
int low = 0, high = n - 1, mid; // Initialize low and high for a sorted array
|
| 92 |
+
int result = -1; // To store the minimum index of the key
|
| 93 |
+
|
| 94 |
+
// Perform binary search
|
| 95 |
+
while (low <= high) {
|
| 96 |
+
mid = (low + high) / 2; // Calculate the middle index
|
| 97 |
+
|
| 98 |
+
if (arr[mid] == key) {
|
| 99 |
+
result = mid; // Save the current index as a potential result
|
| 100 |
+
high = mid - 1; // Continue searching in the left half for a smaller index
|
| 101 |
+
}
|
| 102 |
+
else if (arr[mid] > key) {
|
| 103 |
+
high = mid - 1; // Search the left side
|
| 104 |
+
}
|
| 105 |
+
else {
|
| 106 |
+
low = mid + 1; // Search the right side
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
return result; // Return the minimum index of the key, or -1 if not found
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
int sumArr(int* arr, int n, int x) {
|
| 114 |
+
// Define two pointers: low starting at the beginning of the array,
|
| 115 |
+
// and high starting at the end of the array.
|
| 116 |
+
int low = 0, high = n - 1;
|
| 117 |
+
|
| 118 |
+
// Loop until the two pointers meet.
|
| 119 |
+
while (low < high) {
|
| 120 |
+
// Calculate the sum of the elements at the low and high pointers.
|
| 121 |
+
int sum = arr[low] + arr[high];
|
| 122 |
+
|
| 123 |
+
// If the sum equals the target value x:
|
| 124 |
+
if (sum == x) {
|
| 125 |
+
// Print the indices of the two elements that sum to x.
|
| 126 |
+
printf("%d %d\n", low, high);
|
| 127 |
+
// Return 1 to indicate a successful match.
|
| 128 |
+
return 1;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// If the sum is greater than x, decrement the high pointer
|
| 132 |
+
// to reduce the sum (move to a smaller element).
|
| 133 |
+
if (sum > x)
|
| 134 |
+
high--;
|
| 135 |
+
|
| 136 |
+
// If the sum is less than x, increment the low pointer
|
| 137 |
+
// to increase the sum (move to a larger element).
|
| 138 |
+
if (sum < x)
|
| 139 |
+
low++;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// If no such pair is found, return 0.
|
| 143 |
+
return 0;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
int sumMonotonic(int x, int* arr, int n) {
|
| 147 |
+
int low = 0, high = n - 2;
|
| 148 |
+
int mid, sum = 0;
|
| 149 |
+
|
| 150 |
+
while (low <= high) {
|
| 151 |
+
mid = (low + high) / 2;
|
| 152 |
+
sum = arr[mid] + arr[mid + 1];
|
| 153 |
+
|
| 154 |
+
if (sum == x) return mid;
|
| 155 |
+
|
| 156 |
+
if (sum > x) {
|
| 157 |
+
high = mid - 1;
|
| 158 |
+
}
|
| 159 |
+
else {
|
| 160 |
+
low = mid + 1;
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
return -1;
|
| 164 |
+
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
int main()
|
| 168 |
+
{
|
| 169 |
+
// 1
|
| 170 |
+
//int arr[] = {-1,1,6,9,11};
|
| 171 |
+
//int arr1[] = {-1,0,6,9,11};
|
| 172 |
+
|
| 173 |
+
//printf("%d\n", findIndex(arr,5));
|
| 174 |
+
//printf("%d\n", findIndex(arr1,5));
|
| 175 |
+
//printf("--------\n");
|
| 176 |
+
//printf("%d\n", findIndexLog(arr,5));
|
| 177 |
+
//printf("%d\n", findIndexLog(arr1,5));
|
| 178 |
+
|
| 179 |
+
// 2
|
| 180 |
+
//int arr[] = { 9,7,7,5,3,3,3,1 };
|
| 181 |
+
//int n = 8;
|
| 182 |
+
//printf("%d\n",findKey(arr,n,3));
|
| 183 |
+
//printf("%d\n",findKey(arr,n,4));
|
| 184 |
+
//printf("%d\n", findKeyR(arr, n, 3));
|
| 185 |
+
//printf("%d\n", findKeyR(arr, n, 4));
|
| 186 |
+
|
| 187 |
+
// 3
|
| 188 |
+
//int arr[] = { 1,1,3,3,3,6,8,9,9 };
|
| 189 |
+
//int n = 9;
|
| 190 |
+
//printf("%d\n",findKeyAsc(arr,n,3));
|
| 191 |
+
//printf("%d\n",findKeyAsc(arr,n,4));
|
| 192 |
+
|
| 193 |
+
// 4
|
| 194 |
+
//int arr[] = { 1,3,5,5,6,9,10,13 };
|
| 195 |
+
//int n = 8;
|
| 196 |
+
//int x = 13;
|
| 197 |
+
//printf("%d", sumArr(arr, n, x));
|
| 198 |
+
|
| 199 |
+
// 5
|
| 200 |
+
//int arr[] = {-3,8,1,9,15,10,18};
|
| 201 |
+
//int n = 7;
|
| 202 |
+
//printf("%d\n", sumMonotonic(28, arr, n)); // --> 5
|
| 203 |
+
//printf("%d\n", sumMonotonic(10, arr, n)); // --> 2
|
| 204 |
+
//printf("%d\n", sumMonotonic(5, arr, n)); // --> 0
|
| 205 |
+
|
| 206 |
+
return 0;
|
| 207 |
+
}
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-11.c
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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0; i < n; i++)
|
| 7 |
+
{
|
| 8 |
+
if (i + 1 == n)
|
| 9 |
+
printf("%d", arr[i]);
|
| 10 |
+
else
|
| 11 |
+
printf("%d ,", arr[i]);
|
| 12 |
+
}
|
| 13 |
+
printf("}\n");
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
void sort(int* arr, int n) {
|
| 17 |
+
int freq[] = { 0,0,0,0,0,0,0,0,0,0 }; // Frequency array to count occurrences of digits 0�9
|
| 18 |
+
|
| 19 |
+
// Step 1: Count the frequency of each number in the array (O(n))
|
| 20 |
+
for (int i = 0; i < n; i++) {
|
| 21 |
+
freq[arr[i]]++; // Increment the count of the number at index `arr[i]`
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// Step 2: Reconstruct the sorted array using the frequency array (O(n))
|
| 25 |
+
for (int i = 0, j = 0; i < 10; i++) { // Loop through digits 0�9
|
| 26 |
+
while (freq[i] > 0) { // If the current digit `i` appears in the array
|
| 27 |
+
arr[j++] = i; // Place it in the sorted array
|
| 28 |
+
freq[i]--; // Decrement the frequency count
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
void sortEvenOdd(int* arr, int n) {
|
| 34 |
+
int low = 0, high = n - 1; // Two pointers: `low` starts at the beginning, `high` starts at the end
|
| 35 |
+
int temp; // Temporary variable for swapping
|
| 36 |
+
|
| 37 |
+
// Step 1: Traverse the array using the two pointers
|
| 38 |
+
while (low <= high) {
|
| 39 |
+
// Case 1: `low` points to an odd number, `high` points to an even number
|
| 40 |
+
if (arr[low] % 2 != 0 && arr[high] % 2 == 0) {
|
| 41 |
+
// Swap the odd number at `low` with the even number at `high`
|
| 42 |
+
temp = arr[low];
|
| 43 |
+
arr[low] = arr[high];
|
| 44 |
+
arr[high] = temp;
|
| 45 |
+
|
| 46 |
+
// Move both pointers inward after the swap
|
| 47 |
+
low++;
|
| 48 |
+
high--;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Case 2: `low` points to an even number
|
| 52 |
+
if (arr[low] % 2 == 0) low++; // Move `low` to the next position
|
| 53 |
+
|
| 54 |
+
// Case 3: `high` points to an odd number
|
| 55 |
+
if (arr[high] % 2 != 0) high--; // Move `high` to the previous position
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
void merge(int* arr, int start1, int end1, int start2, int end2) {
|
| 60 |
+
// Calculate the total size of the merged array
|
| 61 |
+
int size = (end1 - start1 + 1) + (end2 - start2 + 1);
|
| 62 |
+
|
| 63 |
+
// Pointers for the two sub-arrays
|
| 64 |
+
int i = start1, j = start2;
|
| 65 |
+
|
| 66 |
+
// Allocate memory for a temporary array to hold the merged result
|
| 67 |
+
int* temp = (int*)calloc(size, sizeof(int));
|
| 68 |
+
if (temp == NULL) { // Check for memory allocation failure
|
| 69 |
+
printf("Memory allocation failed.\n");
|
| 70 |
+
return;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
int count = 0; // Counter for the temporary array
|
| 74 |
+
|
| 75 |
+
// Step 1: Merge elements from both sub-arrays into `temp` while both have elements
|
| 76 |
+
while (i <= end1 && j <= end2) {
|
| 77 |
+
if (arr[i] < arr[j]) { // Take the smaller element from the first sub-array
|
| 78 |
+
temp[count] = arr[i];
|
| 79 |
+
i++; // Move the pointer for the first sub-array
|
| 80 |
+
}
|
| 81 |
+
else { // Take the smaller element from the second sub-array
|
| 82 |
+
temp[count] = arr[j];
|
| 83 |
+
j++; // Move the pointer for the second sub-array
|
| 84 |
+
}
|
| 85 |
+
count++; // Move the pointer for the temporary array
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// Step 2: Add remaining elements from the first sub-array, if any
|
| 89 |
+
while (i <= end1) {
|
| 90 |
+
temp[count] = arr[i];
|
| 91 |
+
i++;
|
| 92 |
+
count++;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
// Step 3: Add remaining elements from the second sub-array, if any
|
| 96 |
+
while (j <= end2) {
|
| 97 |
+
temp[count] = arr[j];
|
| 98 |
+
j++;
|
| 99 |
+
count++;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// Step 4: Copy the merged result from `temp` back to the original array `arr`
|
| 103 |
+
for (int k = 0; k < size; k++) {
|
| 104 |
+
arr[start1 + k] = temp[k];
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// Step 5: Free the memory allocated for `temp`
|
| 108 |
+
free(temp);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
void sortK(int* arr, int n, int k) {
|
| 112 |
+
int start = 0, end = n - 1; // `start` is the beginning of the first sub-array, `end` is the last index of the array
|
| 113 |
+
int end1; // Stores the end index of the first sub-array in each merge
|
| 114 |
+
int start2, end2; // Stores the start and end indices of the second sub-array in each merge
|
| 115 |
+
|
| 116 |
+
// Loop through the array, processing chunks of size `k`
|
| 117 |
+
for (int i = 1; i * k < n; i++) {
|
| 118 |
+
// Calculate the end of the first sub-array
|
| 119 |
+
end1 = (i * k) - 1;
|
| 120 |
+
|
| 121 |
+
// Calculate the start of the second sub-array
|
| 122 |
+
start2 = end1 + 1;
|
| 123 |
+
|
| 124 |
+
// Calculate the end of the second sub-array
|
| 125 |
+
// Ensure that the end of the second sub-array doesn't go beyond the array bounds
|
| 126 |
+
end2 = (start2 + k - 1 > end) ? end : start2 + k - 1;
|
| 127 |
+
|
| 128 |
+
// Merge the two sub-arrays: `arr[start...end1]` and `arr[start2...end2]`
|
| 129 |
+
merge(arr, start, end1, start2, end2);
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Helper function to partition the array and find the sum of k smallest elements
|
| 134 |
+
int partitionHelper(int* arr, int n, int k) {
|
| 135 |
+
// Handle invalid input (k cannot be greater than n)
|
| 136 |
+
if (k > n) {
|
| 137 |
+
printf("Invalid input: k cannot be greater than n\n");
|
| 138 |
+
return 0;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
// Base case: If the array has only one element and k == 1, return that element
|
| 142 |
+
if (n == 1 && k == 1) {
|
| 143 |
+
return arr[0];
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// Step 1: Select a pivot randomly to avoid biased splits
|
| 147 |
+
int pivot = arr[rand() % n];
|
| 148 |
+
|
| 149 |
+
// Allocate memory for two groups
|
| 150 |
+
int* group1 = (int*)malloc(n * sizeof(int)); // Group for elements smaller than pivot
|
| 151 |
+
int* group2 = (int*)malloc(n * sizeof(int)); // Group for elements greater than or equal to pivot
|
| 152 |
+
|
| 153 |
+
if (group1 == NULL || group2 == NULL) { // Check for memory allocation failure
|
| 154 |
+
printf("Memory allocation failed.\n");
|
| 155 |
+
exit(1);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
int count1 = 0, count2 = 0;
|
| 159 |
+
|
| 160 |
+
// Step 2: Partition the array into group1 and group2
|
| 161 |
+
for (int i = 0; i < n; i++) {
|
| 162 |
+
if (arr[i] < pivot) {
|
| 163 |
+
group1[count1++] = arr[i];
|
| 164 |
+
}
|
| 165 |
+
else {
|
| 166 |
+
group2[count2++] = arr[i];
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Step 3: Handle edge case: If group1 is empty, move one instance of pivot to group1
|
| 171 |
+
if (count1 == 0) {
|
| 172 |
+
group1[count1++] = pivot;
|
| 173 |
+
count2--;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// Step 4: Compare the size of group1 with k
|
| 177 |
+
if (count1 == k) {
|
| 178 |
+
// Exactly k smallest elements are in group1
|
| 179 |
+
int sum = 0;
|
| 180 |
+
for (int i = 0; i < count1; i++) {
|
| 181 |
+
sum += group1[i];
|
| 182 |
+
}
|
| 183 |
+
free(group1);
|
| 184 |
+
free(group2);
|
| 185 |
+
return sum;
|
| 186 |
+
}
|
| 187 |
+
else if (count1 > k) {
|
| 188 |
+
// The k smallest elements are all in group1, recurse into group1
|
| 189 |
+
free(group2);
|
| 190 |
+
int result = partitionHelper(group1, count1, k);
|
| 191 |
+
free(group1);
|
| 192 |
+
return result;
|
| 193 |
+
}
|
| 194 |
+
else {
|
| 195 |
+
// Take all elements of group1 and find the remaining (k - count1) smallest elements in group2
|
| 196 |
+
int sum = 0;
|
| 197 |
+
for (int i = 0; i < count1; i++) {
|
| 198 |
+
sum += group1[i];
|
| 199 |
+
}
|
| 200 |
+
free(group1); // Free group1 memory
|
| 201 |
+
int remainingSum = partitionHelper(group2, count2, k - count1);
|
| 202 |
+
free(group2); // Free group2 memory
|
| 203 |
+
return sum + remainingSum; // Return combined sum
|
| 204 |
+
}
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
int findKSmallestSum(int* arr, int n, int k) {
|
| 208 |
+
return partitionHelper(arr, n, k);
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
// Function to merge two sorted sub-arrays in descending order
|
| 212 |
+
void mergeDescending(int* arr, int start, int mid, int end) {
|
| 213 |
+
// Step 1: Calculate the size of the temporary array
|
| 214 |
+
int size = end - start + 1; // Total elements in the range [start, end]
|
| 215 |
+
int* temp = (int*)malloc(size * sizeof(int)); // Allocate temporary array
|
| 216 |
+
if (temp == NULL) {
|
| 217 |
+
printf("Memory allocation failed.\n");
|
| 218 |
+
return;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
// Step 2: Initialize pointers for the left, right sub-arrays, and temp array
|
| 222 |
+
int i = start; // Pointer for the left sub-array [start, mid]
|
| 223 |
+
int j = mid + 1; // Pointer for the right sub-array [mid + 1, end]
|
| 224 |
+
int k = 0; // Pointer for the temporary array
|
| 225 |
+
|
| 226 |
+
// Step 3: Merge the two sub-arrays into temp in descending order
|
| 227 |
+
while (i <= mid && j <= end) {
|
| 228 |
+
if (arr[i] > arr[j]) { // Compare elements
|
| 229 |
+
temp[k++] = arr[i++]; // Take the larger element
|
| 230 |
+
}
|
| 231 |
+
else {
|
| 232 |
+
temp[k++] = arr[j++]; // Take the larger element
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
// Step 4: Copy any remaining elements from the left sub-array
|
| 237 |
+
while (i <= mid) {
|
| 238 |
+
temp[k++] = arr[i++]; // Add remaining elements from the left
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
// Step 5: Copy any remaining elements from the right sub-array
|
| 242 |
+
while (j <= end) {
|
| 243 |
+
temp[k++] = arr[j++]; // Add remaining elements from the right
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
// Step 6: Copy the sorted elements from temp back into the original array
|
| 247 |
+
for (int l = 0; l < size; l++) {
|
| 248 |
+
arr[start + l] = temp[l]; // Copy temp array to the original array
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
// Free the temporary array
|
| 252 |
+
free(temp); // Prevent memory leak
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// Recursive function to sort an array in descending order
|
| 256 |
+
void sortDescendingRecursive(int* arr, int start, int end) {
|
| 257 |
+
if (start >= end) return; // Base case: Sub-array with 1 or 0 elements is already sorted
|
| 258 |
+
|
| 259 |
+
int mid = (start + end) / 2; // Calculate the middle index
|
| 260 |
+
|
| 261 |
+
// Recursively sort the left half
|
| 262 |
+
sortDescendingRecursive(arr, start, mid);
|
| 263 |
+
|
| 264 |
+
// Recursively sort the right half
|
| 265 |
+
sortDescendingRecursive(arr, mid + 1, end);
|
| 266 |
+
|
| 267 |
+
// Merge the two halves in descending order
|
| 268 |
+
mergeDescending(arr, start, mid, end);
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
// Wrapper function to sort the entire array in descending order
|
| 272 |
+
void sortDecending(int* arr, int n) {
|
| 273 |
+
int start = 0, end = n - 1; // Set the range for the entire array
|
| 274 |
+
sortDescendingRecursive(arr, start, end); // Start the recursive sorting process
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
int main()
|
| 278 |
+
{
|
| 279 |
+
// 1
|
| 280 |
+
//int arr[] = { 1,5,9,2,3 };
|
| 281 |
+
//sort(arr, 5);
|
| 282 |
+
//printArr(arr, 5);
|
| 283 |
+
//int arr1[] = { 8,6,5,2,1,3,4,7,8,9,6,5,2,3,5,4,7,0};
|
| 284 |
+
//sort(arr1, 18);
|
| 285 |
+
//printArr(arr1, 18);
|
| 286 |
+
|
| 287 |
+
// 2
|
| 288 |
+
//int arr[] = { 1,6,3,8,2 };
|
| 289 |
+
//sortEvenOdd(arr, 5);
|
| 290 |
+
//printArr(arr, 5);
|
| 291 |
+
|
| 292 |
+
// 3
|
| 293 |
+
//int arr[] = { -1,2,5,3,7,9,4,6 };
|
| 294 |
+
//int n = 8, k = 3;
|
| 295 |
+
//sortK(arr, n, k);
|
| 296 |
+
//printArr(arr, n);
|
| 297 |
+
|
| 298 |
+
//// Test Case 1: Basic Case
|
| 299 |
+
//int arr1[] = { 3, 4, 5, 1, 2, 6 };
|
| 300 |
+
//int n1 = 6, k1 = 3;
|
| 301 |
+
//sortK(arr1, n1, k1);
|
| 302 |
+
//printf("Test 1: ");
|
| 303 |
+
//printArr(arr1, n1); // Expected: {1, 2, 3, 4, 5, 6}
|
| 304 |
+
|
| 305 |
+
//// Test Case 2: Single Element Sub-Arrays
|
| 306 |
+
//int arr2[] = { 10, 20, 30, 40 };
|
| 307 |
+
//int n2 = 4, k2 = 1;
|
| 308 |
+
//sortK(arr2, n2, k2);
|
| 309 |
+
//printf("Test 2: ");
|
| 310 |
+
//printArr(arr2, n2); // Expected: {10, 20, 30, 40}
|
| 311 |
+
|
| 312 |
+
//// Test Case 3: Sub-Arrays Already Sorted
|
| 313 |
+
//int arr3[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
|
| 314 |
+
//int n3 = 8, k3 = 3;
|
| 315 |
+
//sortK(arr3, n3, k3);
|
| 316 |
+
//printf("Test 3: ");
|
| 317 |
+
//printArr(arr3, n3); // Expected: {1, 2, 3, 4, 5, 6, 7, 8}
|
| 318 |
+
|
| 319 |
+
//// Test Case 4: Array with Last Sub-Array Smaller than k
|
| 320 |
+
//int arr4[] = { 9, 12, 14, 1, 3, 8, 7 };
|
| 321 |
+
//int n4 = 7, k4 = 3;
|
| 322 |
+
//sortK(arr4, n4, k4);
|
| 323 |
+
//printf("Test 4: ");
|
| 324 |
+
//printArr(arr4, n4); // Expected: {1, 3, 7, 8, 9, 12, 14}
|
| 325 |
+
|
| 326 |
+
//// Test Case 5: Array of Size 1
|
| 327 |
+
//int arr5[] = { 5 };
|
| 328 |
+
//int n5 = 1, k5 = 3;
|
| 329 |
+
//sortK(arr5, n5, k5);
|
| 330 |
+
//printf("Test 5: ");
|
| 331 |
+
//printArr(arr5, n5); // Expected: {5}
|
| 332 |
+
|
| 333 |
+
//// Test Case 6: Reverse Sorted Array
|
| 334 |
+
//int arr6[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
|
| 335 |
+
//int n6 = 9, k6 = 3;
|
| 336 |
+
//sortK(arr6, n6, k6);
|
| 337 |
+
//printf("Test 6: ");
|
| 338 |
+
//printArr(arr6, n6); // Expected: {1, 2, 3, 4, 5, 6, 7, 8, 9}
|
| 339 |
+
|
| 340 |
+
//// Test Case 7: Array of Repeated Elements
|
| 341 |
+
//int arr7[] = { 5, 5, 5, 5, 5, 5 };
|
| 342 |
+
//int n7 = 6, k7 = 2;
|
| 343 |
+
//sortK(arr7, n7, k7);
|
| 344 |
+
//printf("Test 7: ");
|
| 345 |
+
//printArr(arr7, n7); // Expected: {5, 5, 5, 5, 5, 5}
|
| 346 |
+
|
| 347 |
+
//// Test Case 8: Array with Negative Numbers
|
| 348 |
+
//int arr8[] = { -9, -7, -5, -1, -3, -4, -2 };
|
| 349 |
+
//int n8 = 7, k8 = 3;
|
| 350 |
+
//sortK(arr8, n8, k8);
|
| 351 |
+
//printf("Test 8: ");
|
| 352 |
+
//printArr(arr8, n8); // Expected: {-9, -7, -5, -4, -3, -2, -1}
|
| 353 |
+
|
| 354 |
+
//// Test Case 9: Array Size Smaller than k
|
| 355 |
+
//int arr9[] = { 5, 1, 3, 2 };
|
| 356 |
+
//int n9 = 4, k9 = 5;
|
| 357 |
+
//sortK(arr9, n9, k9);
|
| 358 |
+
//printf("Test 9: ");
|
| 359 |
+
//printArr(arr9, n9); // Expected: {1, 2, 3, 5}
|
| 360 |
+
|
| 361 |
+
//// Test Case 10: Array with k = 1
|
| 362 |
+
//int arr10[] = { 7, 3, 9, 1, 5, 4 };
|
| 363 |
+
//int n10 = 6, k10 = 1;
|
| 364 |
+
//sortK(arr10, n10, k10);
|
| 365 |
+
//printf("Test 10: ");
|
| 366 |
+
//printArr(arr10, n10); // Expected: {1, 3, 4, 5, 7, 9}
|
| 367 |
+
|
| 368 |
+
// 4
|
| 369 |
+
//int arr1[] = { 1, 6, 3, 9, 6, 7 };
|
| 370 |
+
//int n1 = 6, k1 = 3;
|
| 371 |
+
//printf("Sum of %d smallest elements: %d\n", k1, findKSmallestSum(arr1, n1, k1));
|
| 372 |
+
//// Expected Output: 10 (1 + 3 + 6)
|
| 373 |
+
|
| 374 |
+
//int arr2[] = { 5, 4, 3, 2, 1 };
|
| 375 |
+
//int n2 = 5, k2 = 2;
|
| 376 |
+
//printf("Sum of %d smallest elements: %d\n", k2, findKSmallestSum(arr2, n2, k2));
|
| 377 |
+
//// Expected Output: 3 (1 + 2)
|
| 378 |
+
|
| 379 |
+
// 5
|
| 380 |
+
//int arr[] = { 1,6,3,8,2 };
|
| 381 |
+
//int n = 5;
|
| 382 |
+
//sortDecending(arr, n);
|
| 383 |
+
//printArr(arr, n);
|
| 384 |
+
return 0;
|
| 385 |
+
}
|