Show the result of running Shellsort on the input 9, 8, 7, 6, 5, 4, 3, 2, 1 using increment {1, 3, 7}
时间: 2023-10-09 12:10:51 浏览: 105
Shellsort is an in-place comparison sort algorithm that starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Here are the steps to sort the input {9, 8, 7, 6, 5, 4, 3, 2, 1} using increment {1, 3, 7}:
1. First, we sort the elements with a gap of 7:
{1, 8, 7, 6, 5, 4, 3, 2, 9}
2. Next, we sort the elements with a gap of 3:
{1, 2, 7, 6, 5, 4, 3, 8, 9}
3. Finally, we sort the elements with a gap of 1:
{1, 2, 3, 6, 5, 4, 7, 8, 9}
The sorted array is {1, 2, 3, 4, 5, 6, 7, 8, 9}.
相关问题
Write a C++ program. Count the number of occurrences of each letter of the input string, case – insensitive.Wr
ite a C++ program that counts the number of occurrences of each letter of an input string, case-insensitive. Here's one way you could do it:
```cpp
#include <iostream>
#include <string>
#include <map>
#include <cctype>
int main() {
std::string input;
std::getline(std::cin, input); // read input from user
std::map<char, int> letterCounts; // map to store letter counts
for (char c : input) {
if (std::isalpha(c)) { // ignore non-letter characters
char lowercase = std::tolower(c); // convert to lowercase
letterCounts[lowercase]++; // increment count of this letter
}
}
// print out the results
for (auto pair : letterCounts) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
```
This program starts by reading in a string from the user using `std::getline()`. It then creates a `std::map<char, int>` called `letterCounts` to store the counts of each letter. The `for` loop iterates over each character in the input string, checking if it is a letter using `std::isalpha()`. If it is a letter, it converts it to lowercase using `std::tolower()` and increments the count of that letter in the `letterCounts` map.
Finally, the program prints out the results by iterating over the `letterCounts` map and printing out each letter and its count. Note that the `auto` keyword is used to simplify the loop syntax - `pair` is automatically inferred to be a `std::pair<char, int>`.
This program should work for any input string, regardless of its length or the specific letters it contains.
Write codes of the vertical microinstructions for Basic Computer that increment the value of the data register by one (DR = DR+1). You can use several microinstructions.
Here are the vertical microinstructions for Basic Computer that increment the value of the data register by one:
1. MAR <- 0
2. MBR <- DR
3. AC <- MBR + 1
4. DR <- AC
5. PC <- PC + 1
Explanation:
1. Load the memory address register (MAR) with the value 0, indicating we want to access the memory location where the data register (DR) is located.
2. Load the memory buffer register (MBR) with the current value of the DR.
3. Add 1 to MBR using the accumulator (AC).
4. Store the new value back into the data register (DR).
5. Increment the program counter (PC) to the next instruction.