Reverse Quick Sort [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to perform quick sort on a given array in decreasing order but the first number output is always some arbitrary number like 456752.
I am unable to identify the source of problem. Please help
#include <iostream>
#include <cstdlib>
using namespace std;
long randPartition(long a[],long start,long end0)
{
long pivot = start + rand()%(end0 - start);
//swap a[end] with [pivot]
long temp = a[end0];
a[end0] = a[pivot];
a[pivot] = temp;
//now partitioning
long i = start;
for(int j = start; j < end0 ; j++)
{
if(a[j] > a[end0])
{
long temp1 = a[j];
a[j] = a[i];
a[i] = a[temp1];
i++;
}
}
//swapping pivot with its correct position
long temp2 = a[end0];
a[end0] = a[i];
a[i] = temp2;
return i;
}
void quickSort(long ar[],long start,long end0)
{
if (start < end0)
{
long i = randPartition(ar,start,end0);
quickSort(ar,start,i-1);
quickSort(ar,i+1,end0);
}
}
int main()
{
int testCases;
cin >> testCases;
while(testCases--)
{
long size0;
cin >> size0;
long arr[size0];
for(int i = 0; i < size0; i++)
{
cin >> arr[i];
}
//cin >> endl;
//using quick sort algo
quickSort(arr, 0, size0 - 1);
//printing the sorted array
for(int j = 0; j < size0; j++)
{
cout << arr[j] << " ";
}
cout << endl;
}
return 0;
}

Inside randomPartition() At the line with
a[i] = a[temp1];
replace it with
a[i] = temp1;
just a booboo :P

quickSort(arr, 0, size0 - 1); and j < end0 exclude your last value.

Related

Code not working trying to do bubble sort in different way [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 11 months ago.
Improve this question
#include <iostream>
using namespace std;
int main() {
int n, arr[n];
cin >> n;
int i, j;
int counter = 1;
for (i = 0; i < n; i++) {
cin >> arr[i]; // taking arr inputs
}
while (counter < n) {
for (i = 0; i < n - counter; i++) {
if (arr[i] > arr[i + 1]) {
j = arr[i]; // swapping numbers
arr[i] = arr[i + 1];
arr[i + 1] = j;
}
}
counter++;
}
}
my code is simply exiting it isnt taking any inputs or giving any outputs or errors.
i just want to do it this way lemme know what are the mistakes dont change the method
I tried changing conter into loop but it didnt work
tryin bubble sort
We beginners need to hold together.
There are some very minor problems in your code.
Your variable "n" is uninitialized. So it has an indeterminate value. So, something. Then you try to set the size of your array with this "something". And after that, you read the size from the user.
Additionally. VLA (Variable length Arrays), so something like "array[n]", with "n" not being a compile time constant is illegal. It is not part of C++. Some compiler are allowing it as an extension, but it is illegal C++ code. But the good message is: There is a drop in replacement: The std::vector
And last but not least, if you want to see some output, then you need to output something. So, for example with "std::cout << something".
With above changes, your code coul look like:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n = 0;
cin >> n;
std::vector<int> arr(n);
int i = 0, j = 0;
int counter = 1;
for (i = 0; i < n; i++) {
cin >> arr[i]; // taking arr inputs
}
while (counter < n) {
for (i = 0; i < n - counter; i++) {
if (arr[i] > arr[i + 1]) {
j = arr[i]; // swapping numbers
arr[i] = arr[i + 1];
arr[i + 1] = j;
}
}
counter++;
}
for (int k : arr) std::cout << k << ' ';
}
Still not perfect, but OK for the beginning.

How to fix segmentation fault in this program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Im solving this question but cannot get correct output it show segmentation fault
https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/agitated-chandan/description/
This is the code that i write for this program
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int visited[100001], dist[100001];
int largest;
int t, x, y, w, i, z, k;
int large;
cin >> t;
while (t--)
{
int large = 0;
int n;
cin >> n;
vector<pair<int, int>> adj[n];
for (i = 1; i < n; i++)
{
cin >> x >> y >> w;
adj[x].push_back(make_pair(y, w));
adj[y].push_back(make_pair(x, w));
}
queue<int> q;
for (k = 1; k <= n; k++)
{
q.push(k);
for (i = 1; i <= n; i++)
{
visited[i] = -1;
dist[i] = 0;
}
largest = 0;
while (!q.empty())
{
z = q.front();
visited[z] = 1;
q.pop();
for (i = 0; i < adj[z].size(); i++)
{
if (visited[adj[z][i].first] == -1)
{
dist[adj[z][i].first] = dist[z] + adj[z][i].second;
if (largest < dist[adj[z][i].first])
{
largest = dist[adj[z][i].first];
}
q.push(adj[z][i].first);
visited[adj[z][i].first] = 1;
}
}
}
if (large < largest)
{
large = largest;
}
}
if (large < 100)
{
cout << 0 << " ";
}
if (large > 100 && large < 1000)
{
cout << 100 << " ";
}
if (large > 1000 && large < 10000)
{
cout << 1000 << " ";
}
if (large > 10000)
{
cout << 10000 << " ";
}
cout << large;
}
}
I expect output be
0 8
Just heck the logic is it write or wrong
One possible cause is that you store indexes from 1 to n to the queue q:
for (k = 1; k <= n; k++) {
q.push(k);
and then use them (as z) for indexing adj of size n:
while (!q.empty()) {
z = q.front(); // z equals n in the first iteration here
...
for (i = 0; i < adj[z].size(); i++) // adj[n] will be accessed
Indexes in C++ are zero-based, therefore the valid ones are from 0 to n-1.

Below Code is not printing anything [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int ma(float array[], int N)
{
int k = 0;
float max = array[k];
for (int i = 0; i < N; ++i) {
if (array[i] > max) {
max = array[i];
k = i;
}
}
return k;
}
int main()
{
int t;
while (t--) {
int n;
cin >> n;
int w[n], p[n];
for (int i = 0; i < n; i++)
cin >> w[i];
for (int i = 0; i < n; i++)
cin >> p[i];
float x[n];
for (int i = 0; i < n; i++)
x[i] = p[i] / w[i];
int weigth = 0, profit = 0;
while (weigth <= 20) {
// int k=distance(x, max_element(x, x + n));
// int k= std::distance(x, max_element(x, x + sizeof(x)/sizeof(x)));
int k = ma(x, n);
weigth = weigth + w[k];
profit = profit + p[k];
x[k] = p[k] = w[k] = 0;
}
cout << weigth << endl
<< profit << endl;
}
}
The above code is not printing anything. If you want the question refer to "catch-the-match":
your code is not even compiling,
you can not do this in C++
int n;
cin >> n;
int w[n], p[n];
because n must be a constant at compiling time, on the other hand doing this:
int t;
while (t--) {
is producing an unpredictable number of iterations in the loop since t is not initialized
You have to declare int n as a const int n in order for you code to compile correctly.

Increasing array size in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have project and this is my code. I am expected to write a console application which finds the largest k numbers in a given file and prints these largest k numbers in descending order. My program should take the filename and k as input parameters from the user.
For example for the following file content:
3, 5, 12, 54, 12, 3, 654, 11, 46, 7, 3
The output for k = 3 should be:
654 54 46
using namespace std;
int main() {
int i, size, p, maxim, k, n, j;
int* a = new int[7000000];
size = 7000000;
ifstream file("7_million_numbers.txt");
if (file.is_open())
{
for (i = 0; i < size; i = i + 1)
file >> a[i];
}
cout << "Enter the number: " << endl;
cin >> n;
for (j = 1; j < n; j = j + 1) {
for (k = 0; k < size - 1; k++) {
maxim = a[k];
p = k;
for (i = k + 1; i < size; i++)
if (a[i] > maxim) {
maxim = a[i];
p = i;
}
a[p] = a[k];
a[k] = maxim;
}
}
for (i = 0; i < n; i++)
cout << a[i] << " " << endl;
system("pause");
delete[] a;
return 0;
}
This code is not working. I had ".exe stopped working" problem. Is it because of an array size? Because text file which is reading by program has a 7 million numbers.
I have a very simple solution to your problem, you can sort the given numbers in descending order before taking the input of the numbers to be shown, after sorting you can ask user "How many greatest numbers to be shown" and when you show the sorted result it will automatically show the greatest n numbers.
Instead of int make it long int.
int main() {
int i, size, p, maxim, k, n, j;
long int* a = new long int[7000000];
size = 7000000;
ifstream file("7_million_numbers.txt");
if (file.is_open())
{
for (i = 0; i < size; i = i + 1)
file >> a[i];
}
cout << "Enter the number: " << endl;
cin >> n;
for(j=0;j<size;j++){
for(k=j;k<size;k++){
if(a[j]<a[k]){
int temp;
temp = a[j];
a[j] = a[k];
a[k] = temp;
}
}
}
for (i = 0; i < n; i++)
cout << a[i] << " " << endl;
system("pause");
delete[] a;
return 0;
}
It seems to me that you're only doing one lookup per run. I would think that you wouldn't need to store all the numbers in memory just the kth largest. to do this a set<int> would work well:
void FindKthLargest( istream& fileIn , ostream& userOut, istream& userIn )
{
int k = 0;
userOut << "Enter the k number: ";
userIn >> k;
set<int> largetNums;
int temp = 0;
for ( int i = 0; i < k; i++ )
{
if ( fileIn >> temp )
{
largetNums.emplace( temp );
}
else
{
userOut << "Number is too big\n";
return;
}
}
while ( fileIn >> temp )
{
set<int>::iterator lowest = largetNums.begin();
if ( temp > *lowest )
{
largetNums.emplace( temp );
largetNums.erase( lowest );
}
}
for ( auto i : largetNums )
{
userOut << i;
}
}

How to display numbers by line depending on the number in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi I am new to C++ and I am having problem with displaying the numbers from 1,2,3, and so on depending on the user input.
For example, if input is 3, first line of output should be 1, next line should be 2 3, and the last line should be 4 5 6. Please see below screenshot:
The number of elements to display in the leftmost n columns is exactly:
So, the numbers in the first row form the A000124 integer sequence.
Therefore, you can just add the row index to respective value of the sequence, and print it only when row index is not greater than column index.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(i <= j) {
cout << j*(j+1)/2+1 + i;
}
cout << '\t';
}
cout << endl;
}
return 0;
}
See the code live.
I hope this helps.
#include<iostream>
using namespace std;
int main()
{
int n, l=1;
cin >> n;
for(int j = 1; j <= n; j++) {
int k = l;
for(int i = 1; i <= n; i++) {
if(i < j) {
cout << " \t";
continue;
}
cout << k << "\t";
k += i;
}
cout << endl;
l = l + j + 1;
}
return 1;
}