Created
March 20, 2025 15:47
-
-
Save vlaleli/fe714601cffb4d5424caf87fb671e5b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| void shuffleArray(int arr[], int size) | |
| { | |
| for (int i = size - 1; i > 0; i--) | |
| { | |
| int j = rand() % (i + 1); | |
| int temp = arr[i]; | |
| arr[i] = arr[j]; | |
| arr[j] = temp; | |
| } | |
| } | |
| void printArray(const int arr[], int size) | |
| { | |
| for (int i = 0; i < size; i++) | |
| { | |
| cout << arr[i] << " "; | |
| } | |
| cout << endl; | |
| } | |
| int findElement(const int arr[], int size, int value) | |
| { | |
| for (int i = 0; i < size; i++) | |
| { | |
| if (arr[i] == value) | |
| { | |
| return i; | |
| } | |
| } | |
| return -1; | |
| } | |
| void sortAscending(int arr[], int start, int end) | |
| { | |
| for (int i = start; i < end - 1; i++) | |
| { | |
| for (int j = start; j < end - (i - start) - 1; j++) | |
| { | |
| if (arr[j] > arr[j + 1]) | |
| { | |
| int temp = arr[j]; | |
| arr[j] = arr[j + 1]; | |
| arr[j + 1] = temp; | |
| } | |
| } | |
| } | |
| } | |
| void sortDescending(int arr[], int start, int end) | |
| { | |
| for (int i = start; i < end - 1; i++) { | |
| for (int j = start; j < end - (i - start) - 1; j++) | |
| { | |
| if (arr[j] < arr[j + 1]) | |
| { | |
| int temp = arr[j]; | |
| arr[j] = arr[j + 1]; | |
| arr[j + 1] = temp; | |
| } | |
| } | |
| } | |
| } | |
| int main() | |
| { | |
| srand(time(0)); | |
| const int SIZE = 20; | |
| int arr[SIZE]; | |
| for (int i = 0; i < SIZE; i++) | |
| { | |
| arr[i] = i + 1; | |
| } | |
| shuffleArray(arr, SIZE); | |
| cout << "After shuffle: "; | |
| printArray(arr, SIZE); | |
| int randomNumber = rand() % 20 + 1; | |
| cout << "Random number: " << randomNumber << endl; | |
| int pos = findElement(arr, SIZE, randomNumber); | |
| sortDescending(arr, 0, pos); | |
| sortAscending(arr, pos + 1, SIZE); | |
| cout << "Modified array: "; | |
| printArray(arr, SIZE); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment