// algo assignment.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
 using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int DATA[5];
	DATA[0]=0;
	DATA[1]=10;
	DATA[2]=80;
	DATA[3]=1;
	DATA[4]=5;
	int a;int temp; int i,j; 

	
	for (i=0;i<5;i++)
	{     
	 temp=DATA[i];  // at starting it means that it has value zero
		for (j=i+1;j<5;j++)
		{
			
		  if(temp>DATA[j] )
		   
		  { temp=DATA[j];
		  
		  a=DATA[j-1];
		  
		  DATA[j-1]=DATA[j];

		  a=DATA[j];}
		}
		  
		  
		cout <<DATA[i]<<endl;
	
	
	}
	
			
			
			
			
			
			
			system("PAUSE");

the main purpose of this program is that i just want to sort the array in an order and it is not going like that

Dani AI

Generated

Quick diagnosis: the code mixes behaviors from selection sort and bubble sort. The inner loop is changing temp and swapping adjacent elements (DATA[j-1] / DATA[j]) while it should either (a) repeatedly swap adjacent items for a true bubble pass, or (b) find the minimum index and swap once for a selection pass. That incorrect swapping plus printing inside the outer loop produces the wrong order. was right to point out the mix, and ’s advice to study a single algorithm first is the right approach; ’s “do it on paper” tip is also very effective for spotting index mistakes.

Use a single clear algorithm. Here is a compact, correct selection-sort example in C++ you can drop into a console program:

#include <iostream>
#include <algorithm>

int main() {
    int data[] = {0, 10, 80, 1, 5};
    int n = sizeof(data)/sizeof(data[0]);

    for (int i = 0; i < n - 1; ++i) {
        int minIdx = i;
        for (int j = i + 1; j < n; ++j)
            if (data[j] < data[minIdx]) minIdx = j;
        if (minIdx != i) std::swap(data[i], data[minIdx]);
    }

    for (int i = 0; i < n; ++i) std::cout << data[i] << '\n';
    return 0;
}

Practical debugging tips: (1) Trace the array on paper for two outer iterations to see what your swaps do. (2) Print the whole array after each outer pass (not mid-inner-loop) to see progress. (3) Use std::swap or a single temp-based swap between i and minIdx for selection sort. For production code prefer std::vector and std::sort once you understand the basics.

Recommended Answers

All 8 Replies

What sort algorithm are you using in this case? Even though it looks like a bubble sort to me, I am still not sure.

i dont know all i am doing is that i am sorting it out that all with my own failed logic

Then you should read the link in my previous post and look at the algorithm (pseudo code). Compare that to yours and adjust it. Then your sort should be OK. I don't understand how an instructor would give you a sorting assignment without giving or discussing about it at all...

well i self studying programming no one is teaching me

OK, then you need to read the link I posted in my first post first before you try to implement a sort function. ;)

As suggested, the error is in your sorting...

Seems like you have mixed Bubble Sort and Selection sort algorithms.

You just do your code on paper.. then you will find out already. *When you do, must follow code not follow what you planned.

It's a sequence problem...

thanks guy i have done it thank you so very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.