array sort ascending order - bubble sort - c++

I'm trying to arrange my array in an asscending order but its giving me a different value:
#include <iostream>
int main ()
{
const int size = 5;
// 0 1 2 3 4
int arr[size] = {50,20,54,12,23};
for (int i=0; i<5; i++) //! bubble sort ascending order
{
for (int j=i; j<5; j++)
{ // 50 20
if (arr[j] > arr[j+1])
{
int temp = arr [j+1];
arr [j+1] = arr [j];
arr [j] = temp;
}
}
}
for (int i=0; i<5; i++)
{
std::cout<<arr[i]<<" ";
}
return 0;
}
The fact that it is not working, could it be of the empty array value (arr[6]) at the end of the loop that is messing up with the overall value? If so, it should be able to sort up till the last array size but its giving a different output instead
Output: 20 12 0 23 50

To prevent out of bound problems, change:
i<5 to size-1
j<5 to size-1-i
j=i to j=0
Code:
int main ()
{
const int size = 5;
// 0 1 2 3 4
int arr[size] = {50,20,54,12,23};
for (int i=0; i<size-1; i++) //! bubble sort ascending order
{
for (int j=0; j<size-1-i; j++)
{ // 50 20
if (arr[j] > arr[j+1])
{
int temp = arr [j+1];
arr [j+1] = arr [j];
arr [j] = temp;
}
}
}
for (int i=0; i<5; i++)
{
std::cout<<arr[i]<<" ";
}
return 0;
}

Related

Why this code is working fine at leetcode but giving segmentation fault at geeksforgeeks?

gfg https://practice.geeksforgeeks.org/problems/subset-sum-problem2014/1
leetcode
https://leetcode.com/problems/partition-equal-subset-sum/
Problem:
Given an array arr[] of size N, check if it can be partitioned into two parts such that the sum of elements in both parts is the same.
Example
Input: N = 4
arr = {1, 5, 11, 5}
Output: YES
Explaination:
The two parts are {1, 5, 5} and {11}.
class Solution{
public:
static int equalPartition(int N, int arr[])
{
int sum = 0;
for (int i=0; i<N; i++)
sum += arr[i];
if (sum % 2 != 0)
return 0;
sum = sum/2;
int row = N+1;
int col = sum+1;
int dp[row][col];
for (int i=0; i<col; i++)
dp[0][i] = 0;
for (int i=0; i<row; i++)
dp[i][0] = 1;
for (int i=1; i<row; i++) {
for (int j=1; j<col; j++) {
if ( j< arr[i-1])
dp[i][j] = dp[i-1][j];
else{
if(j-arr[i-1] > 0){
dp[i][j] =max(dp[i-1][j], dp[i-1][j-arr[i-1]]);
}
else{
dp[i][j] = dp[i-1][j];
}
}
}
}
return dp[row-1][col-1];
}
};
So I signed up to portal out of curiosity to figure whats wrong. I was able to get correct answer with the following code. The code makes slight changes, mostly space compression.
As I suspected, the constraints for N (100) and sum of values in array (1e5) were very critical leading to segmentation fault.
Also instead of j-arr[i-1] > 0, it should be j-arr[i-1] >= 0.
Explanation of space compression:
We only need i-1 th state to compute values of current state i, so 2 arrays of size sum each are enough. I keep a reference curr to remember which of the 2 array is to be considered current and curr^1 would be the previous array. We can further optimize to only single array of size sum, but it is out of scope of the answer.
int equalPartition(int N, int arr[])
{
int sum = 0;
for (int i=0; i<N; i++)
sum += arr[i];
if (sum % 2 != 0)
return 0;
sum = sum/2;
int row = N+1;
int col = sum+1;
int dp[2][col]; // change 1
for (int i=0; i<col; i++)
dp[0][i] = 0;
// for (int i=1; i<row; i++)
// dp[i][0] = 1;
dp[0][0] = 1; // change 2, due to space compression don't need above
int curr = 1; // change 3
for (int i=1; i<row; i++, curr^=1) { // change 4
for (int j=1; j<col; j++) {
dp[curr][j] = dp[curr^1][j]; // change 5
if(j-arr[curr^1] >= 0) // change 6
dp[curr][j] =max(dp[curr][j], dp[curr^1][j-arr[i-1]]); // change 7
}
}
return dp[curr^1][col-1]; // change 8
}
};

Delete duplicates from array C++

I am trying to create a simple program which calls on 2 functions. The first function takes a partially filled array, loops through it and deletes any duplicate values. When a value is deleted from the array, the remaining numbers are moved backwards to fill the gap i.e. when the function is finished, all null values of the array will be together at the end.
The second function prints the updated array.
My current code is below. At present when I run my code, the console shows:
2 6 0 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460. It should be showing: 1 2 5 6 if it worked correctly.
Also, I am not sure how to move the remaining elements of the array backwards so that the null values will be together at the end.
#include "pch.h"
#include <iostream>
using namespace std;
void deleteRepeats(int *arr, int arraySize, int& posUsed);
void printArray(int *arr, int arraySize);
int main()
{
int arr[10] = { 1, 2, 2, 5, 6, 1};
int posUsed = 6;
int arraySize = 10;
deleteRepeats(arr, arraySize, posUsed);
printArray(arr, arraySize);
return 0;
}
void deleteRepeats(int *arr, int arraySize, int& posUsed)
{
{
for (int i = 0; i < arraySize; i++)
{
for (int j = i; j < arraySize; j++)
{
if (arr[i] == arr[j])
{
for (int k = j; k < arraySize; k++)
{
arr[k] = arr[k + 1];
}
posUsed--;
}
else
j++;
}
}
}
}
void printArray(int *arr, int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
cout << arr[i] << " ";
}
}
I would let the std containers to what you like to do.
Sort the vector
Use erase and unique to delete duplicates.
Here is the code
#include <vector>
#include <iostream>
#include <algorithm>
void print(const std::vector<int> &arr){
for (const auto & i : arr){
std::cout << i <<" ";
}
std::cout <<"\n";
}
int main() {
std::vector<int> arr{1, 2, 2, 5, 6, 1};
print(arr);
std::sort( arr.begin(), arr.end() );
arr.erase( std::unique( arr.begin(), arr.end() ), arr.end() );
print(arr);
}
Ps. Using int *arr, int arraySize is not very C++ like. Please always try to use a proper container (which almost always will be std::vector).
EDIT:
I changed my answer a bit, because I found this speed comparison (and actuallty the whole question answered).
What's the most efficient way to erase duplicates and sort a vector?
Given your assignment constraints (more C-like, than idiomatic C++), you can rewrite your function like this, to make it work:
void deleteRepeats(int *arr, int arraySize, int& posUsed)
{
for (int i = 0; i < posUsed; ++i)
{
int duplicates = 0;
int j = i + 1;
// find the first duplicate, if exists
for ( ; j < posUsed; ++j)
{
if ( arr[i] == arr[j] ) {
++duplicates;
break;
}
}
// overwrite the duplicated values moving the rest of the elements...
for (int k = j + 1; k < posUsed; ++k)
{
if (arr[i] != arr[k])
{
arr[j] = arr[k];
++j;
}
// ...but skip other duplicates
else
{
++duplicates;
}
}
posUsed -= duplicates;
}
// clean up (could be limited to the duplicates only)
for (int i = posUsed; i < arraySize; ++i)
arr[i] = 0;
}
It might be easier to imagine the algorithm having separate input and output arrays. Then, in pseudo-code:
for i = 0 to input_array_size-1
Is input[i] equal to input[j] for any j between 0 and i-1?
Yes - do nothing
No - copy input[i] to output
To implement this with shared input and output, you need to have two array sizes, input_array_size and output_array_size. Then, the pseudo-code becomes
output_array_size = 0
for i = 0 to input_array_size-1
Is array[i] equal to array[j] for any j between 0 and output_array_size-1?
Yes - do nothing
No:
copy array[i] to array[output_array_size]
Increase output_array_size
Note: it writes output where the input once was, so the check for duplicates should look at all elements that were output. For example, if your array is 1, 2, 1, 3, 5, 6, 3, then for the last 3 the accumulated output is 1, 2, 3, 5, 6, and the code should compare all these with the current element.
To simplify debugging, where it says "do nothing", you can set current element to -1. This way, if you print your array during execution (for debugging), it will be clearer which elements were removed.
there are only two changes made as you can see
1: you were traversing the whole array as you have declared a posUsed=6 variable which is because there are only 6 elements so in in loops you need to traverse in array upto posUsed index like i<posUsed j<posUsed k<posUsed
2: the second changes is in j loop j=i+1 because you don't need to compare the element of any index with element of the same index you have to compare it with elements after that index. if you compare it with same element it will be same and the program will delete that same element which results in ERROR.
onw more thing is that we don't traverse after posUsed index because after that the array is already empty/zero or null whatever you call it
and if you want to display just the non duplicated elements and not the zero's at the end of the array just add if(arr[i]==0) return; in the printArray function loop before cout statement
void deleteRepeats(int *arr, int arraySize, int& posUsed)
{
{
for (int i = 0; i < posUsed; i++)
{
for (int j = i+1; j < posUsed; j++)
{
if (arr[i] == arr[j])
{
for (int k = j; k < posUsed; k++)
{
arr[k] = arr[k + 1];
}
}
}
}
}
}
using two pointers
and if the array sorted
int removeDuplicates(vector<int>& nums) {
if(nums.size() == 0) return 0;
int i = 0;
for(int j = 1; j < nums.size(); j++)
if(nums[j] != nums[i]) nums[++i] = nums[j];
// return new array length
return i + 1;
}
//input: [1, 1, 2, 1] (arr1)
//output: 2 (returned length)
// print unique element
for(int i = 0; i < output; i++) cout << arr1[i] << '\n';
// [1, 2]
time complexity: O(N/2) -> O(N)
space complexity: O(1)
Removing duplicate elements from an unsorted array by O(n^2) complexity.
for (i = 1; i < vec.size(); i++)
{
for (j = 0; j < i; j++)
{
if (vec[i] == vec[j])
{
vec[i] = -1; //Every duplicate element will replace by -1
}
}
}
for (i = 0; i < vec.size(); i++)
{
if (vec[i] != -1)
{
copy.push_back(vec[i]);
/*if you are using an array then store this value into a new array.
first, declare a new array. The new array size will be equal to the
previous array. Like this :
int newArr[sizeOfPreviousArrary];
int j = 0;
newArr[j] = arr[i];
j++;
*/
}
}
Removing duplicate elements from an sorted array by O(n) complexity.
for (i = 0; i < n; i++)
{
if (arr[i] != arr[i+1]){
vec.push_back(arr[i]);
/*if you are using an array then store this value into a new array.
first, declare a new array. The new array size will be equal to the
previous array. Like this :
int newArr[sizeOfPreviousArrary];
int j = 0;
newArr[j] = arr[i];
j++;
*/
}
}
Use map or set for deleting duplicates
void removeDuplicates(int arr[], int n)
{
int i;
// Initialise a set
// to store the array values
set<int> s;
// Insert the array elements
// into the set
for (i = 0; i < n; i++) {
// insert into set
s.insert(arr[i]);
}
set<int>::iterator it;
// Print the array with duplicates removed
cout << "\nAfter removing duplicates:\n";
for (it = s.begin(); it != s.end(); ++it)
cout << *it << ", ";
cout << '\n';
}

Getting segmentation fault (vector declaration)

Find out the maximum sub-array of non negative numbers from an array.
The sub-array should be continuous. That is, a sub-array created by choosing the second and fourth element and skipping the third element is invalid.
Maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is greater than sub-array B if sum(A) > sum(B).
This is my solution:
vector<int> Solution::maxset(vector<int> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
vector <int> bla;
int sum[100]={0};
int k = 0;
int j = 1;
for (int i =0; i < A.size(); i++){
if (A[i] > -1){
sum[k] = A[i] + sum[k];
}
else {
k++;
}
}
cout<<sum[0]<<" ";
cout<<sum[1]<<" ";
cout << sum[2] << " ";
int s = 0;
for (int i =0; i< 100; i++){
if (s < sum[i]){
s = sum[i];
k = i;
}
}
cout << s;
int count = 0;
for (int i =0; i < A.size(); i++){
if (A[i] < 0) {
count ++;
}
if (count == k) {
int j = i+1;
int x = 0;
while (A[j] > 0 && j< (A.size()-1)) {
// bla[x] = A[j];
x++;
j++;
}
}
}
return bla;
}
If I uncomment the line bla[x] = A[j], I get segmentation error. Can someone explain how to undetstand this error? I read it somewhere that there is not enough space in stack. I do not understand how.
Thank you
You can pass the size to a vector object or you can call it's default constructor which creates a vector object with 0 size.
std::vector<int> vecInt(10);
for(int i(0); i < vecInt.size(); i++)
vecInt[i] = i;
Or you can declare a vector with size 0:
std::vector<int> vecInt;
vecInt[0] = 10; // segfault
Because you try to store values in an un-allocated space.
To solve such problem use push_back to store and pop to clear:
So your example can be like this:
while (A[j] > 0 || j< (A.size()-1)) {
// bla[x] = A[j];
bla.push_back(A[j]);
x++;
j++;
}

arranging array in ascending order

I'm trying to arrange a random array. In the code it's just the first step of swapping places by size.
When running the code a get Debug Error and the output after the swap shows that the first number in the array was deleted and the last is a long random number that was in the memory.
It looks like it starts the swapping from i=1, why?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void buildArray(int arr[], int size) {
srand ( (unsigned int)time(NULL) );
for(int i = 0; i < size; ++i)
arr[i] = rand() % 50 + 0;
}
void dispArray(int arr[], int size) {
for(int i = 0; i < size; ++i)
cout << i << ": " << arr[i] << endl;
}
int main()
{
const int size = 5;
int arr[size];
buildArray(arr, size);
dispArray(arr, size);
int swapHolder = -1;
for(int i = 0; i < size; ++i) {
if(arr[i] > arr[i+1]) {
swapHolder = arr[i+1];
arr[i+1] = arr[i];
arr[i] = swapHolder;
cout << endl;
}
}
dispArray(arr, size);
return 0;
}
Output example:
0: 46
1: 15
2: 47
3: 5
4: 19
0: 15
1: 46
2: 5
3: 19
4: -858993460
Here is the problem: if(arr[i] > arr[i+1]), your for loop goes from [0, size - 1], so on the last iteration, lets say size = 5, you would be testing if(arr[5] > arr[6]), accessing the uninitialized arr[6], the right way is to make your for loop go from [0, size - 2]:
for(int i = 0; i < size - 1; ++i) ...
for(int i = 0; i < size; ++i) {
if(arr[i] > arr[i+1]) {
swapHolder = arr[i+1];
arr[i+1] = arr[i];
The last iteration is for i = size -1. Then
if(arr[i] > arr[i+1])
means accessing array out of range. This is undefined behavior.

Selection sort ascending

That is my function:
int main() {
double data[100];
int num;
cout<<"num= ";
cin>>num;
for(int i = 1; i <= num; i++) {
cout<<i<<" element = ";
cin>>data[i];
}
Sort(data, num);
for (int i = 1; i <= num; i++) {
cout<<data[i]<<endl;
}
return 0;
}
void Sort(double data[], int n) {
int i,j,k;
double min;
for(i = 0; i < n-1; i++) {
k = i;
min = data[k];
for(j = i+1; j < n; j++)
if(data[j] < min) {
k = j;
min = data[k];
}
data[k] = data[i];
data[i] = min;
}
}
if I write for exp. three elements: 8,9,1 again cout 8,9,1?
for(int i = 1; i <= num; i++) { // WRONG
I think you mean:
for(int i = 0; i < num; i++) { // RIGHT
Arrays in C are 0-indexed remember.
Your sorting function is fine. The only problem is that you enter elements at positions 1 through n, inclusive, while you should use 0 through n-1, inclusive, in both loops of the main() function.
If you need to print numbers 1 through n, use
cout<<(i+1)<<" element = ";
You should get used of the 0 index begin in the for loop
for(int i = 0; i < N; ++i)
so fixing these two index errors will make your code run properly.
the reason is:
if you write data to data[] using 1 as the begining, your data array's first item will be a random number:
if you insert 3 elements, the array will be like this:
data[0] = ??? // maybe a very very big number
data[1] = 8
data[2] = 9
data[3] = 1
and in your Sort function, your index begins at 0 and ends before num, that means your code would only sort data[0], data[1], data[2].
if you use: num = 3, 3 2 1 as your input data for the origin code you could see that 3 and 2 is sorted
I guess your Sort code is googled from somewhere, please try to understand it.
Good online algorithm course: https://www.coursera.org/course/algs4partI
a very good algorithm online book: http://algs4.cs.princeton.edu/home/
btw, for(j = i+1; j < n; j++) in the Sort function would be better if it has { } braces.