Selection Sort

I Write a selection sort algorithm that takes teh lowest item from an

Page 1 of 1

0 Replies - 897 Views - Last Post: 29 November 2007 - 06:04 AM Rate Topic: -----

#1 a1i100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-November 07

Selection Sort

Posted 29 November 2007 - 06:04 AM

I have the been working on this code for a while. Everything is fine except when i try to run the program i always get one error. The FindItemIndex is undefined. HOwever, i have tried to define it but it still does not work. Please HELP!!


#include <iostream.h>
#include "Utility.h"
#include <h:/lvp/vector.h>


typedef int ItemType;
typedef vector<ItemType> ArrayType;
int NumItems =20;

//----------------------------------------------------------------
void LoadArray(ArrayType &A)
/*Loads all array locations with random numbers from 0 to 99*/
{
for(int i =0; i<A.length(); i++)
A[i]=RandomInRange(1,100);
}
//--------------------------------------------------------------------
int FindLowest(ArrayType &A)
{
int i,j;
int temp;
for (i=0;i<NumItems-1;i++)
for(j=0;j<NumItems-1;j++)
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
int Lowest;
Lowest = A[0];
return(Lowest);
}

//--------------------------------------------------------------------
void AddToArray(ArrayType &Array, const ItemType &ValToAdd)
/*Increases size of Array by 1 and adds ValToAdd to Aray
as the last element
Post: Array length increased by 1 and ValToAdd is last element of array*/
{
Array.resize(Array.length()+1);
Array[Array.length()-1] = ValToAdd;
}
//-----------------------------------------------------------------------
void RemoveFromArray(ArrayType &Array,const ItemType &ValToDelete)
/*Deletes ValToDelete from Array and decreases size of Array by 1
Pre: Array is of size >=1. ValToDelete is present in Array.
Post: Array length has decreased by 1 and ValToDelete is removed*/

{

int Index=FindItemIndex(Array,ValToDelete);
for(;Index<Array.length()-1;Index++)
Array[Index]=Array[Index+1];
Array.resize(Array.length()-1);
}
//-----------------------------------------------------------------------

int main()
{
randomize();
const int NumItems =20;
ArrayType A(NumItems);
LoadArray(A);
FindLowest(A);
cout<<A[0];
cout<<endl;
/*PrintArray(A);
Sort(A);
PrintArray(A);*/
return(0);
}

Is This A Good Question/Topic? 0
  • +

Page 1 of 1