Problem with selection sort

Hi,
I was trying to do insertion sort for the array data[5]={3,1,4,7,5}. But always '7' comes as the first element and the rest of it is sorted correctly. the program I had written was this:
int selsort(int data[], int n)
{
int temp,min_id=0,min=0,i;
for(i=0; i<=n-1; i++)
min=data[i];
{
for (int j=i+1; j<n; j++)
if(data[j]<min)
{
min=data[j];
min_id=j;
}
temp=data[i];
data[i]=data[min_id];
data[min_id]=temp;

}
//for printing the elements:
for (int k =0; k<=n-1; k++)
{
cout << data[k]<<endl;
}
cin.get();
return 0;
}


can someone please help?
Thanks...
I was trying to do insertion sort for the array data[5]={3,1,4,7,5}


which sort do you want us to help you insertion or selection?
I am sorry for the mistake in the question. I want help with selection sorting.
Selection for each n in array, iterate n-1
Insertion is much better for small arrays, and arrays that store mostly sorted data.
http://www.algolist.net/Algorithms/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void selectionSort(int arr[], int n) {
      int i, j, minIndex, tmp;    
      for (i = 0; i < n - 1; i++) {
            minIndex = i;
            for (j = i + 1; j < n; j++)
                  if (arr[j] < arr[minIndex])
                        minIndex = j;
            if (minIndex != i) {
                  tmp = arr[i];
                  arr[i] = arr[minIndex];
                  arr[minIndex] = tmp;
            }
      }
}


1
2
3
4
5
6
7
8
9
10
11
12
void insertionSort(int arr[], int length) {
      int i, j, tmp;
      for (i = 1; i < length; i++) {
            j = i;
            while (j > 0 && arr[j - 1] > arr[j]) {
                  tmp = arr[j];
                  arr[j] = arr[j - 1];
                  arr[j - 1] = tmp;
                  j--;
            }
      }
}
Last edited on
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<cctype>
using namespace std;

void SelectionSort(int Array[],int);

int main()
{ 
     int size, *Array;
 
     cout<<"\t\t\t###### SELECTION SORT ######\n"<<endl;

     cout<<"Please Enter the size of the array:";
     cin>>size;
     Array=new int[size];
 
    for(int i=0;i<size;i++)
    {
		cout<<"\n\nEnter Element "<<i + 1<<":";
		cin>>*(Array + i);
	}		
	
	cout<<"Initial Order of elements"<<endl;
	for(int j=0;j<size;j++)
	{
		cout<<Array[j]<<" ";
	}
	cout<<endl;
	
	SelectionSort( Array, size);

	cout<<"Final Array After Sorting With Selection Sort\n"<<endl;
	cout<<"##############################################\n"<<endl;

	for(int i=0;i<size;i++)
	{
		cout<<Array[i]<<"	";
	}
	return 0;
}
void SelectionSort(int Array[],int size)
{   
	int temp;
	int smallest;

	for(int i=0;i<size;i++)
	{
		smallest=i;
		{
			for(int j=i+1;j<size;j++)
			{
				if(Array[smallest]>Array[j])
				{
					smallest=j;
				}
				if(smallest !=i)
				{
					temp = Array[i];
					Array[i] = Array[smallest];
					Array[smallest] = temp;
				}
			}
		}
	}
}

Hope this will help you
Topic archived. No new replies allowed.