I've been working on this , need help

Hey guys , I've been working on this and I really need help to sort an array , please help and also please , just please don't post any codes..

I was trying to solve it and I spent about one hour but I needed to post this to get some help because I need to study other subjects and I got no time :(

it sort like a charm for the first few elements , until element 3 , I think the problem is in the else statement since if it's not > it will print swap " the last swap when the if statement was true " and then prints the element after which would be the same.

what do you guys think ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include <string.h>
using namespace std;


int main(){
const int size=5;
int array[size]={5,4,73,2,1};
int swap=0;

for (int i=0; i<size; i++){
	for (int j=i+1; j<size; j++){
		if (array[i]>array[j]){
			swap=array[j]; 
			array[j]=array[i]; 
			array[i]=swap; 

		}
		else {
			swap=array[i];
		}
		
	}
	array[i]=swap;
}
for (int a=0; a<size; a++){           //ignore this , this only shows the element's value after sorting
	cout<<array[a]<<" ";
}


cin.get();
cin.get();

	return 0;
}
closed account (Dy7SLyTq)
i would
a) use <cstring> not string.h
b) why are you including <string> <iomanip> and <string.h>?
c) have you tried implementing bubble swap?
About including files , I usually when I try something I try it on the same subject that I opened before "
I used this project for a lot of things :D

I'm too lazy to open a new one.

anyways I didn't understand your point c ?
edit: I overreacted, but `cin.get()' and `return 0' should be at the same indentation level


You've got a bad version of selection sort. It would be a lot easier if you modularize the code.
14
15
16
17
18
19
20
	for (int i=0; i<size; i++){
		//in the last element the loop would not execute
		for (int j=i+1; j<size; j++){
			//...
		}
		array[i]=swap; //swap has an invalid value
	}




> it will print swap " the last swap when the if statement was true "
there is no such thing in the code that you posted.
Last edited on
Thanks buddy , I know that there's an algorithm to solve this easily , but I wanted to do it myself instead of using that algorithm.

anyways I think it works now , it compares every single element now and gives a swap a value if the first element was 0

I tried it on a few element , changed from here to there and it still works.

Thanks a lot of helping guys , I'll try to test a few times and see what happens , I hope it doesn't give any logic errors :(

this is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include <string.h>
using namespace std;


int main(){
const int size=5;
int array[size]={900,41,88,69,77};
int swap=0;

for (int i=0; i<size; i++){
	for (int j=0; j<size; j++){
	
		if (array[i]<array[j] ){
			swap=array[j]; 
			array[j]=array[i]; 
			array[i]=swap; 

		}
	else {
		swap=array[i];
	}

		
		
	}
	array[i]=swap;
}
for (int a=0; a<size; a++){
	cout<<array[a]<<" ";
}


cin.get();
cin.get();

	return 0;
}
Last edited on
First of all, the nested loop in your bubble sort starts in the wrong place, it should start from the index next to the first one, as in, j should always be the index next to i, so if i = 0, j should be 1, if i = 3, j should be 4. And why don't you just use the built in swap function?
the nested loop in your bubble sort starts in the wrong place,


How so ?

so if i = 0, j should be 1, if i = 3, j should be 4.


I've tried that , the problem is this causes it to swap the value and then print it again.

And why don't you just use the built in swap function?


there's one ? what's the form ?

..

Thanks a lot , I just wanted to do this myself , and it works " the last code I posted " I tried it on 5 elements array and 10 elements array and it still works.
Topic archived. No new replies allowed.