# Array sort, smallest to largest

Page 1 of 1

## 5 Replies - 20711 Views - Last Post: 07 December 2012 - 11:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=299438&amp;s=66860ecc4672402cd58a670da030c7d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 castigero

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 16-October 12

# Array sort, smallest to largest

Posted 09 November 2012 - 10:21 AM

I'm making an array sort using pointers and don't understand why it's in an infinite loop, I'm sure I'm missing something simple. Any help would be greatly appreciated.

my code
```#include <cstdlib>
#include <iostream>
#include <iomanip>

/*
Program sorts an array of integers using a selection sort.
The general algorithm repeatedly finds the smallest number
in the array and places it at the front of the list.
*/

using namespace std;

const int size = 20;

int find_small_ptr (int start_ptr, int numbers []);
void swap_values (int ptr1, int ptr2, int numbers []);
void print (int numbers []);

int main(int argc, char *argv[])
{
// array of numbers
int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,
54, 128, 62, 44, 12, 1023, 89, 905, 32, -12};
int *start_ptr;  // current starting spot for search
int *small_ptr;  // index of the smallest number in the array

start_ptr = 0;
// continue finding the smallest value and placing it
// at the front of the list
while (*start_ptr < size - 1)
{
*small_ptr = find_small_ptr (*start_ptr, numbers);
swap_values (*small_ptr, *start_ptr, numbers);
start_ptr++;
}

cout << "\n\nThe sorted array is:\n";
print (numbers);

cout << "\n\n";

system("PAUSE");
return EXIT_SUCCESS;
}

// finds and returns the index of the smallest number remaining in
// the array
int find_small_ptr (int start_ptr, int numbers [])
{
int *small_ptr, // smallest index to be returned
*ptr,       // current index being viewed
*siz;

*siz = size;
*small_ptr = start_ptr;
// look at each element
for (ptr = small_ptr + 1; ptr < siz; ptr++)
// remember index of smaller value
if (&numbers [*ptr] < &numbers [*small_ptr])
small_ptr = ptr;

return *small_ptr;
}

// swap the values in the array at indexes index1 and index2
void swap_values (int ptr1, int ptr2, int numbers [])
{
int *swapper;

*swapper = numbers [ptr1];
numbers [ptr1] = numbers [ptr2];
numbers [ptr2] = *swapper;
}

// prints the array in nice format, 10 numbers per line
void print (int numbers [])
{
int *on_line,  // number of values printed on the line
*ptr;    // index of current number being printed

on_line = 0;
// print each element in the array
for (ptr = 0; ptr < numbers; ptr++)
{
cout << setw (5) << &numbers [*ptr];
on_line++;
// if 10 numbers have been printed on the line
// go to next line
if (*on_line == 10)
{
cout << "\n";
*on_line = 0;
}
}
}

```

castigero, on 09 November 2012 - 10:20 AM, said:

I'm making an array sort using pointers and don't understand why it's in an infinite loop, I'm sure I'm missing something simple. Any help would be greatly appreciated.

my code
```#include <cstdlib>
#include <iostream>
#include <iomanip>

/*
Program sorts an array of integers using a selection sort.
The general algorithm repeatedly finds the smallest number
in the array and places it at the front of the list.
*/

using namespace std;

const int size = 20;

int find_small_ptr (int start_ptr, int numbers []);
void swap_values (int ptr1, int ptr2, int numbers []);
void print (int numbers []);

int main(int argc, char *argv[])
{
// array of numbers
int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,
54, 128, 62, 44, 12, 1023, 89, 905, 32, -12};
int *start_ptr;  // current starting spot for search
int *small_ptr;  // index of the smallest number in the array

start_ptr = 0;
// continue finding the smallest value and placing it
// at the front of the list
while (*start_ptr < size - 1)
{
*small_ptr = find_small_ptr (*start_ptr, numbers);
swap_values (*small_ptr, *start_ptr, numbers);
start_ptr++;
}

cout << "\n\nThe sorted array is:\n";
print (numbers);

cout << "\n\n";

system("PAUSE");
return EXIT_SUCCESS;
}

// finds and returns the index of the smallest number remaining in
// the array
int find_small_ptr (int start_ptr, int numbers [])
{
int *small_ptr, // smallest index to be returned
*ptr,       // current index being viewed
*siz;

*siz = size;
*small_ptr = start_ptr;
// look at each element
for (ptr = small_ptr + 1; ptr < siz; ptr++)
// remember index of smaller value
if (&numbers [*ptr] < &numbers [*small_ptr])
small_ptr = ptr;

return *small_ptr;
}

// swap the values in the array at indexes index1 and index2
void swap_values (int ptr1, int ptr2, int numbers [])
{
int *swapper;

*swapper = numbers [ptr1];
numbers [ptr1] = numbers [ptr2];
numbers [ptr2] = *swapper;
}

// prints the array in nice format, 10 numbers per line
void print (int numbers [])
{
int *on_line,  // number of values printed on the line
*ptr;    // index of current number being printed

on_line = 0;
// print each element in the array
for (ptr = 0; ptr < numbers; ptr++)
{
cout << setw (5) << &numbers [*ptr];
on_line++;
// if 10 numbers have been printed on the line
// go to next line
if (*on_line == 10)
{
cout << "\n";
*on_line = 0;
}
}
}

```

ignore the "siz" variable haha

Is This A Good Question/Topic? 0

## Replies To: Array sort, smallest to largest

### #2 AKMafia001

• </code.in.dream>

Reputation: 238
• Posts: 738
• Joined: 11-June 11

## Re: Array sort, smallest to largest

Posted 09 November 2012 - 11:02 AM

Before looking any further to your code, I would like to point out something...
```    int *start_ptr;  // current starting spot for search
int *small_ptr;  // index of the smallest number in the array

```

Index of the array can be an int, there is no need for a pointer to int int *small_ptr...

We normally assign 0 to a pointer when we currently don't assign any address to it and wish to assign an address to it later...start_ptr = 0;

Now the problem is here, while (*start_ptr < size - 1) you are dereferencing start_ptr while it doesn't points to any location.

I haven't read the code any further...

Please don't use unnecessary quotes...

Hope this Helps!

### #3 mojo666

Reputation: 408
• Posts: 882
• Joined: 27-June 09

## Re: Array sort, smallest to largest

Posted 09 November 2012 - 11:12 AM

You should go through your code and make sure you are using pointers correctly on every line. When you want the value at a location, you dereference the pointer. When you want the location, you just use the pointer.

Some examples you got wrong.
```  start_ptr = 0; //you really should never set a pointer to a constant location you probably mean
start_ptr = numbers;

while(*start_ptr < size - 1) //while the integer at start_ptr is less than size - 1?  Also, start_ptr doesn't start at 0.  You probably want to do something like...

int *start_arr = numbers
while(start_ptr < start_arr + size - 1)

swap_values (*small_ptr, *start_ptr, numbers);  //swap_values (int at small_ptr, int at start_ptr, numbers)

void swap_values (int ptr1, int ptr2, int numbers [])
{
int *swapper;

*swapper = numbers [ptr1]; //suppose the value at start_ptr is 905.  you store the int at the 905th location of the numbers array at the swapper location.
numbers [ptr1] = numbers [ptr2];
numbers [ptr2] = *swapper;
}

```

hope this helps a bit.

### #4 murume

Reputation: 3
• Posts: 50
• Joined: 21-June 11

## Re: Array sort, smallest to largest

Posted 07 December 2012 - 07:46 AM

Well I'm trying to do something much simpler than this. I'm just trying to sort three integers using a function. When I wrote my code like as shown below no problem:
```#include<stdio.h>
main(){
int tmp, d[3], i, j;

printf("Enter the three numbers:\n");
scanf("%d %d %d", &d[0], &d[1], &d[2]);

for(j=0; j<3; j++){
for(i=0; i<3; i++){
if(d[i]>d[i+1]){
tmp = d[i+1];
d[i+1] = d[i];
d[i] = tmp;
}
}
}

for(i=0; i<3; i++){
printf("%3d", d[i]);
}
}

```

but when I try yo use a function the largest integer is turned into a zero. Below is the code:
```#include<stdio.h>
int tmp, d[3], i, j;
void sort(void);

main(){

printf("Enter the three numbers:\n");
scanf("%d %d %d", &d[0], &d[1], &d[2]);

sort();

for(i=0; i<3; i++){
printf("%3d", d[i]);
}
}

void sort(void){
for(j=0; j<3; j++){
for(i=0; i<3; i++){
if(d[i]>d[i+1]){
tmp = d[i+1];
d[i+1] = d[i];
d[i] = tmp;
}
}
}
}

```

What's wrong with

### #5 AKMafia001

• </code.in.dream>

Reputation: 238
• Posts: 738
• Joined: 11-June 11

## Re: Array sort, smallest to largest

Posted 07 December 2012 - 08:13 AM

Because in the second code you have global variables which are initialized before main() is invoked... For built-in types the default value is 0.

Now the problem is, you are reading out of bounds array location...
Say, i is 2:
```20	        if(d[i]>d[i+1]){
```

This would resolve to if(d[2] > d[3]) where the array indexes are from 0-2... Hence it compares a value which is out of bounds of the array and perhaps initialized to 0 which is less and gets in row...

Hope this Helps!

### #6 murume

Reputation: 3
• Posts: 50
• Joined: 21-June 11

## Re: Array sort, smallest to largest

Posted 07 December 2012 - 11:43 PM

Quite stupid, aint I? thanks very much.