Sorting array problem :)

This is a discussion on Sorting array problem :) within the C++ Programming forums, part of the General Programming Boards category; Hello everyone here. Im new and yes that is my homework im not going to lie. I need to write ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    Sorting array problem :)

    Hello everyone here. Im new and yes that is my homework im not going to lie.
    I need to write a program that doen't exactly sort an array. I will give an example
    If i have that array : 1,3,0,2,0,-1,0,-2,-4,0
    After transformations... the same array must be -1,-2,-4,0,0,0,0,1,3,2
    The numbers<0 in the left part
    The numbers == 0 in the middle
    The numbers > 0 after the nulls in the right part.
    Okay here is the problem. I must use only one array.
    I can do it to make the program to this point -1,-2,-4,0,0,0,0 but with the positive numbers i've got a big problem . Here is my code.
    Code:
    #include <iostream.h>
    
    int main() {
    	const int K=10;
    	int array[K]={1,3,0,2,0,-1,0,-2,-4,0},i,b=0,m;
    	for(i=0;i<K;i++)
    		cout << array[i] << " ";
    	cout << endl;
    	for(i=0;i<K;i++)
    		if(array[i]<0) {
    			m=array[b];
    			array[b]=array[i];
    			array[i]=m;
    			b++;
    		}
    	for(i=b;i<K;i++)
    		if(array[i]==0) {
    			m=array[b];
    			array[b]=array[i];
    			array[i]=m;
    			b++;
    		}
    	for(i=0;i<K;i++)
    		cout << array[i] << " ";
    	cout << endl;
    	return 0;
    }
    and the output is
    Code:
    1 3 0 2 0 -1 0 -2 -4 0
    -1 -2 -4 0 0 0 0 3 1 2
    ... 3 1 2 It must be 1 3 2.
    Sorry for my english. Im not native.
    I would be happy if you give me some ideas.
    Please help me.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Not sure what would be best, but you can probably use bubble-sort with a different swapping condition (if first > 0 and second < 0 then swap).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    10x for the fast reply but i dont need to sort the array in ascending/descending order.
    The numbers in my array must follow this rule : first negative number must be the first number in the array without caring if its smaller/larger than the next and so on.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    This assignment is the first step toward implementing quicksort -- get all the elements less than the pivot to the left, and all of the elements greater than the pivot to the right, where the pivot is 0 in this case.

    The relative order must also be preserved, which means this sort is stable. So what you want to study is a reference implementation of a stable quicksort pivot.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    Oh my Mighty Lord. sufoode your code works for God's sake.
    But what is that memmove thing ?

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    Thank you very much sufoode and to all who read that problem.

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    Okay i wish to thank to all of you once again.
    I used as the other guys said some kind of bubble sort to do the assignment.
    Here is my last code
    (if someone has the same problem)
    Code:
    #include <iostream.h>
    void swap(int& a,int& b);
    int main() {
    	const int K=10;
    	int array[K];
    	int i,j,b=0;
    	bool nulls=false;
    	for(i=0;i<K;i++) {
    		cin >> array[i];
    		if(!cin) { cout << "Error ! Enter integers!\n"; return 1; }
    	}
    	for(i=0;i<K;i++) 
    		if(array[i]==0) nulls=true;
    		else if(array[i]<0) {
    			for(j=i;j>b;j--)
    				swap(array[j],array[j-1]);
    			b++;
    		}
    		if(nulls==true)
    			for(i=b;i<K;i++) 
    				if(array[i]==0) {
    					for(j=i;j>b;j--)
    					swap(array[j],array[j-1]);
    				b++;
    				}
    	for(i=0;i<K;i++)
    		cout << array[i] << ' ';
    	cout << endl;
    	return 0;
    }
    void swap(int& a,int& b) {
    	a=a+b;
    	b=a-b;
    	a=a-b;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21