Write a C++ program that operationalizes the (awful) sort shown in the following flow chart (2 images seen below, look at the second image first (the one that says start) and then the other). You must use this sorting algorithm, with variables scoped as indicated in the flow chart

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section7.1: One-dimensional Arrays
Problem 10E: (Electrical eng.) Write a program that specifies three one-dimensional arrays named current,...
icon
Related questions
Question

Write a C++ program that operationalizes the (awful) sort shown in the following flow
chart (2 images seen below, look at the second image first (the one that says start) and then the other). You must use this sorting algorithm, with variables scoped as indicated in the flow chart

### Flowchart for Sorting Algorithm

#### Left Diagram: Main Procedure

1. **Start**
2. **Step 1:** Initialize integer `n`.
3. **Step 2:** Define integer array `a(i)` for `i=1` to `n`.
4. **Step 3:** Call the `sort` procedure.
5. **Step 4:** Display the sorted array `a(i)` for `i=1` to `n`.
6. **Stop** 

**Legend for Main Procedure:**
- `n`: integer
- `a`: integer array
- `sort`: procedure

#### Right Diagram: Sorting Procedure

1. **Step 1:** Iterate `j` from 1 to `n-1`.
2. **Step 2:** Compare `a(j)` with `a(j+1)`.
   - If `a(j) > a(j+1)`, proceed to Step 3.
3. **Step 3:** Perform the `move` operation (swap `a(j)` and `a(j+1)`).
4. **Return** to Step 1 until all elements are sorted.

**Legend for Sort Procedure:**
- `n`: global integer
- `a`: global array
- `j`: integer in the loop
- `move`: operation to swap elements

### Explanation:
This flowchart represents a basic sorting algorithm, commonly known as a bubble sort. The main procedure initializes the necessary variables and calls the sorting procedure. The sorting procedure iterates over an array of integers, comparing and swapping adjacent elements if they are in the wrong order, until the entire array is sorted.
Transcribed Image Text:### Flowchart for Sorting Algorithm #### Left Diagram: Main Procedure 1. **Start** 2. **Step 1:** Initialize integer `n`. 3. **Step 2:** Define integer array `a(i)` for `i=1` to `n`. 4. **Step 3:** Call the `sort` procedure. 5. **Step 4:** Display the sorted array `a(i)` for `i=1` to `n`. 6. **Stop** **Legend for Main Procedure:** - `n`: integer - `a`: integer array - `sort`: procedure #### Right Diagram: Sorting Procedure 1. **Step 1:** Iterate `j` from 1 to `n-1`. 2. **Step 2:** Compare `a(j)` with `a(j+1)`. - If `a(j) > a(j+1)`, proceed to Step 3. 3. **Step 3:** Perform the `move` operation (swap `a(j)` and `a(j+1)`). 4. **Return** to Step 1 until all elements are sorted. **Legend for Sort Procedure:** - `n`: global integer - `a`: global array - `j`: integer in the loop - `move`: operation to swap elements ### Explanation: This flowchart represents a basic sorting algorithm, commonly known as a bubble sort. The main procedure initializes the necessary variables and calls the sorting procedure. The sorting procedure iterates over an array of integers, comparing and swapping adjacent elements if they are in the wrong order, until the entire array is sorted.
### Flowchart Explanation for Sorting Algorithm

This flowchart describes a sorting process with two main procedures: **Move** and **Findkay**. Each procedure is concerned with the manipulation and sorting of elements within an array `a`. Below is a detailed explanation of each section of the flowchart.

#### Legend
- **a**: in top, global
- **j**: in sort, global
- **temp**: integer, global
- **k**: integer, global
- **sw**: integer

#### Move Procedure
1. **Initialize**: 
    - `temp` is assigned the value of `a(j+1)`.
    - `a(j+1)` is assigned the value of `a(j)`.

2. **Call Findkay**: 
    - The procedure then calls `Findkay`.
    
3. **Assign `temp`**: 
    - `a(k)` is assigned the value of `temp`.

4. **Return**: 
    - The procedure returns to the calling function or ends its execution.

#### Findkay Procedure
1. **Initialize**: 
    - Set `k` equal to `j`, and `sw` to 0.

2. **Loop**:
    - Enters a **while loop** with the condition `(k > 1) and (sw = 0)`.
    - This loop will continue as long as `k` is greater than 1 and `sw` is 0.
    
3. **Comparison**:
    - Check if `a(k-1) > temp`.
    - If **Yes**:
      - Execute step 4.
    - If **No**, go to step 5.

4. **Adjust Array and Decrement**: 
    - Assign `a(k)` the value of `a(k-1)`.
    - Decrement `k` by 1 and loop back to step 2.

5. **Set Switch**: 
    - Assign 1 to `sw`.

6. **Return**: 
    - Exit the while loop and go to the return point to end the procedure.

This flowchart represents a part of a sorting algorithm, likely dealing with an insertion sort or a variant where elements are shifted to correct positions to maintain an ordered list. The `Move` procedure shifts an element upwards and calls `Findkay` to place elements into their correct positions.
Transcribed Image Text:### Flowchart Explanation for Sorting Algorithm This flowchart describes a sorting process with two main procedures: **Move** and **Findkay**. Each procedure is concerned with the manipulation and sorting of elements within an array `a`. Below is a detailed explanation of each section of the flowchart. #### Legend - **a**: in top, global - **j**: in sort, global - **temp**: integer, global - **k**: integer, global - **sw**: integer #### Move Procedure 1. **Initialize**: - `temp` is assigned the value of `a(j+1)`. - `a(j+1)` is assigned the value of `a(j)`. 2. **Call Findkay**: - The procedure then calls `Findkay`. 3. **Assign `temp`**: - `a(k)` is assigned the value of `temp`. 4. **Return**: - The procedure returns to the calling function or ends its execution. #### Findkay Procedure 1. **Initialize**: - Set `k` equal to `j`, and `sw` to 0. 2. **Loop**: - Enters a **while loop** with the condition `(k > 1) and (sw = 0)`. - This loop will continue as long as `k` is greater than 1 and `sw` is 0. 3. **Comparison**: - Check if `a(k-1) > temp`. - If **Yes**: - Execute step 4. - If **No**, go to step 5. 4. **Adjust Array and Decrement**: - Assign `a(k)` the value of `a(k-1)`. - Decrement `k` by 1 and loop back to step 2. 5. **Set Switch**: - Assign 1 to `sw`. 6. **Return**: - Exit the while loop and go to the return point to end the procedure. This flowchart represents a part of a sorting algorithm, likely dealing with an insertion sort or a variant where elements are shifted to correct positions to maintain an ordered list. The `Move` procedure shifts an element upwards and calls `Findkay` to place elements into their correct positions.
Expert Solution
Step 1: Algorithm

Algorithm:
1. Define three functions: Move, Findkay, and Sort.

Function Move(a, j, n):
   1. Create a temporary variable 'temp' and store a[j + 1] in it.
   2. Set a[j + 1] to a[j].
   3. Set a[j] to 'temp'.

Function Findkay(a, j, n):
   1. Initialize k to j and sw to 0.
   2. While k is greater than 0 and sw is 0:
      a. If a[k - 1] is greater than a[k], swap a[k - 1] and a[k], and set sw to 1.
      b. Decrement k by 1.
   3. Return k.

Function Sort(a, n):
   1. Iterate over j from 0 to n - 2:
      a. Call Findkay(a, j, n) and store the result in k.
      b. If k is not equal to j, call Move(a, j, n).

2. In the main function:
   1. Read the number of elements 'n' from the user.
   2. Create an array 'a' of size 'n' and read the elements from the user.
   3. Call Sort(a, n) to sort the array using the given algorithm.
   4. Print the sorted array.

3. End of the algorithm.


steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Algebraic Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning