Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Move Sort algorithm in C Programming

August 25, 2013 Posted by: GPA Views: 606 0 comments

Problem

The Move Sort algorithm will move an identified element to the rear of the array. All elements in between the selected element and the end of the data set will move one position to the left (an index one less than its previous position in the array). Unlike the Selection, Bubble, and Insertion sorting algorithms the Move Sort algorithm will terminate immediately when the array is sorted. There will be no passes through the Move Sort algorithm that fails to make any moves. Given a data set of non-negative integers apply the Move Sort algorithm to order the data from smallest to largest in as few moves as possible. The maximum number of elements input is 20, but the user can enter a -1 to indicate that no more input will follow if less than 20 elements is desired.

Solution

How does it work? Start with looking at the end, and find the smallest number among greatest. For example orginal = 3 2 5 1 6 4; look at the end the number is 4, then both 5,6 greater than 4, smallest number among greatest 5,6 is 5. For detail explanation of this problem look at example below:

Example Execution #1:
Enter data #1: 3
Enter data #2: 5
Enter data #3: 1
Enter data #4: 4
Enter data #5: 6
Enter data #6: 2
Enter data #7: -1
Current data: 3 5 1 4 6 2
Array after move #1: 5 1 4 6 2 3
Array after move #2: 5 1 6 2 3 4
Array after move #3: 1 6 2 3 4 5
Array after move #4: 1 2 3 4 5 6
Number of moves: 4

Description of Example Execution #1:
• The first move identifies that the 3 must be sent to the rear of the array. All other elements shift one position
to the left.
• The second move identifies that the 4 must be sent to the rear of the array. The 6, 2, and 3 elements move one position to the left.
• The third move identifies that the 5 must be sent to the rear of the array and all other elements shift one position to the left.
• The fourth and final move identifies that the 6 must move to the rear of the array and elements 2, 3, 4, and 5 move one position to the left.

 

Example:

   

#include<stdio.h>
#define
SIZE 20 //size of the input array
int getinput_array(int []);
void printArray(int input_array[], int size);
void moveSort(int input_array[], int size);
//main function
int main()
{
   printf("Globalproganswer.com\n\n");
   int input_array[SIZE]; //array containing the spaces available in each section
   int arraysize; //size of the array the user entered
   //get input array
   arraysize = getinput_array(input_array);
   if(arraysize!=20){
      arraysize--;
   }
   printf("\n");
   printArray(input_array, arraysize);
   printf("\n");
   moveSort(input_array, arraysize);
   //delay
   system("PAUSE");
   return(0);
}

It is main function of solution other code you can find in zip file

Output

Move Sort algorithm C

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe