写一个c++冒泡排序例程
时间: 2023-04-06 11:00:50 浏览: 89
好的,我可以回答这个问题。以下是一个简单的 C 冒泡排序例程:
```
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
这个例程使用冒泡排序算法对一个整数数组进行排序。
相关问题
输出一个C语言冒泡排序法例程
Sure, 这是 C 语言实现的冒泡排序法例程:
```
#include <stdio.h>
int main()
{
int arr[6] = {36, 10, 1, 34, 15, -6};
int i, j, temp;
printf("原数组:\n");
for (i = 0; i < 6; i++)
printf("%d ", arr[i]);
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5 - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("\n排序后的数组:\n");
for (i = 0; i < 6; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
```
这个例程的输出是:
```
原数组:
36 10 1 34 15 -6
排序后的数组:
-6 1 10 15 34 36
```
c++ 写一个线程池类并附带例程
好的,下面是线程池类的代码和附带的例程:
代码:
```c++
#include <queue>
#include <thread>
#include <vector>
#include <functional>
#include <condition_variable>
class ThreadPool {
public:
ThreadPool(size_t threads) : stop(false) {
for (size_t i = 0; i < threads; ++i) {
workers.emplace_back([this] {
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
}
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> {
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
if (stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task] { (*task)(); });
}
condition.notify_one();
return res;
}
~ThreadPool() {
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (std::thread &worker : workers)
worker.join();
}
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
```
例程:
```c++
#include <iostream>
#include <chrono>
#include <thread>
#include "ThreadPool.h"
int main() {
ThreadPool pool(4);
std::vector<std::future<int>> results;
for (int i = 0; i < 8; ++i) {
results.emplace_back(
pool.enqueue([i] {
std::cout << "task " << i << " started\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "task " << i << " finished\n";
return i * i;
})
);
}
for (auto && result : results)
std::cout << result.get() << ' ';
std::cout << std::endl;
return 0;
}
```
这个例程中,我们定义了一个线程池,使用大小为4的线程池,并在其中运行8个任务,每个任务都是一个延时1秒的函数,每个任务完成后返回其自身的平方值。运行完毕后,打印输出这8个任务的结果。