Bubble sort using templates

Write a template version of Bubble sort algorithm. The function should look like:
template <class T>
void BubbleSort(T A[], int N)
where A -- array to sort, N -- number of elements in the array

#include <iostream>
using namespace std;

template <class T>
void BubbleSort(T A[], int N){
int i, j, buf;
int N=1000;
for (i=0; i<N-1; i++)
for (j=i+1;j<N;j++)
if (A[i]>arr[j])
{
buf = A[i];
A[i] = A[j];
A[j] = buf;
}
}

Am I on the right track?
if you are accepting N as a parameter why do you reinitialize it?
N should be the number of items in the area which would already be coming from main()


#include <iostream>
using namespace std;

template <class T>
void BubbleSort(T A[], int N){
int i, j, buf;
for (i=0; i<N-1; i++)
{
for (j=i+1;j<N;j++)
{
if (A[i]>arr[j])
{
buf = A[i];
A[i] = A[j];
A[j] = buf;
}
}
}
}
Thanks, so is this complete?

#include <iostream>
using namespace std;
template <class T>
void BubbleSort(T A[], int N){
for (int i=0; i<N-1; i++)
{
for (int j=i+1;j<N;j++)
{
if (A[i]>A[j])
{
int buf;
buf = A[i];
A[i] = A[j];
A[j] = buf;
}
}
}
}

void main(){
int N[10];
for (int i=0; i<10; i++)
N[i]=rand();
}
your sorting function will not run because you did not call it in the main funnction
ok thanks
Topic archived. No new replies allowed.