Sponsored Content
Top Forums Programming Merge sort when array starts from zero(0) index??? Post 302607083 by gabam on Tuesday 13th of March 2012 01:53:24 PM
Old 03-13-2012
Merge sort when array starts from zero(0) index???

Hi friends,
I have implemented the merge sort algorith in c, before I put forward my question, you please have a look at my code.


Code:
 
// The array is sorted, as 1234
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int A[] = {4, 3, 2, 1};
void Merge_Sort(int [], int, int);
void Merge(int [], int, int, int);
int main()
{
    int i;
    printf("Unsorted array\n\n");
    for(i = 0 ; i < 4 ; i++)
    {
        printf("%d\n",A[i]);
    }
    Merge_Sort(A,0,3);
    printf("\nSorted array\n\n");
    for(i = 0 ; i < 4 ; i++)
    {
        printf("%d\n",A[i]);
    }
    printf("\n\n");
    return 0;
}
void Merge_Sort(int A[], int p, int r)
{
    int q;
    if(p < r)
    {
        q = floor((p + r)/2);
        Merge_Sort(A, p, q);
        Merge_Sort(A, q + 1, r);
        Merge(A, p, q, r);
    }
    return 0;
}
void Merge(int A[], int p, int q, int r)
{
    int n1, n2, i, j, k;
    n1 = (q - p) + 1 ;
    n2 = r - q;
    int L[n1 + 1];
    int R[n2 + 1];
    for(i = 1 ; i <= n1 ; i++)
    {
        L[i] = A[(p + i) - 1];
    }
    for(j = 1 ; j <= n2 ; j++)
    {
        R[j] = A[q + j];
    }
    L[n1 + 1] = 999999999;
    R[n2 + 1] = 999999999;
    i = 1;
    j = 1;
    for(k = p ; k <= r ; k++)
    {
        if(L[i] <= R[j])
        {
        A[k] = L[i];
        i = i + 1;
        }
        else
        {
        A[k] = R[j];
        j = j + 1;
        }
    }
    return 0;
}

The above code works perfectly, while the code below doesn't work. Where am I going wrong???


Code:
 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int A[] = {4, 3, 2, 1};
void Merge_Sort(int [], int, int);
void Merge(int [], int, int, int);
int main()
{
    int i;
    printf("Unsorted array\n\n");
    for(i = 0 ; i < 4 ; i++)
    {
        printf("%d\n",A[i]);
    }
    Merge_Sort(A,0,3);
    printf("\nSorted array\n\n");
    for(i = 0 ; i < 4 ; i++)
    {
        printf("%d\n",A[i]);
    }
    printf("\n\n");
    return 0;
}
void Merge_Sort(int A[], int p, int r)
{
    int q;
    if(p < r)
    {
        q = floor((p + r)/2);
        Merge_Sort(A, p, q);
        Merge_Sort(A, q + 1, r);
        Merge(A, p, q, r);
    }
    return 0;
}
void Merge(int A[], int p, int q, int r)
{
    int n1, n2, i, j, k;
    n1 = (q - p) + 1;
    n2 = r - q;
    int L[n1 + 1];
    int R[n2 + 1];
    for(i = 0 ; i < n1 ; i++)
    {
        L[i] = A[(p + i) - 1];
    }
    for(j = 0 ; j < n2 ; j++)
    {
        R[j] = A[q + j];
    }
    L[n1 + 1] = 999999999;
    R[n2 + 1] = 999999999;
    i = 0;
    j = 0;
    for(k = p ; k <= r ; k++)
    {
        if(L[i] <= R[j])
        {
        A[k] = L[i];
        i = i + 1;
        }
        else
        {
        A[k] = R[j];
        j = j + 1;
        }
    }
    return 0;
}

My algorithm doesn't work just because the created arrays start from zero index, could you please help me with this, where am I wrong?
Thanks in advance!
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

2. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

3. UNIX for Advanced & Expert Users

sql variable as array index

hi folks i am facing problom while trying to access sql variable as array index ina unix shell script....script goes as below.. #!/bin/ksh MAX=3 for elem in alpha beeta gaama do arr=$elem ((x=x+1)) Done SQL_SERVER='servername' /apps/sun5/utils/sqsh -S $SQL_SERVER -U user -P pwd -b -h... (1 Reply)
Discussion started by: sudheer157
1 Replies

4. Shell Programming and Scripting

awk array index help

$ cat file.txt A|X|20 A|Y|20 A|X|30 A|Z|20 B|X|10 A|Y|40 Summing up $NF based on first 2 fields, $ awk -F "|" 'BEGIN {OFS="|"} { sum += $NF } END { for (f in sum) print f,sum } ' file.txt o/p: A|X|50 A|Y|60 A|Z|20 (4 Replies)
Discussion started by: uwork72
4 Replies

5. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

6. Shell Programming and Scripting

merge two text files of different size on common index

I have two text files. text file 1: ID filePath col1 col2 col3 1 10584588.mol 269.126 190.958 23.237 2 10584549.mol 281.001 200.889 27.7414 3 10584511.mol 408.824 158.316 29.8561 4 10584499.mol 245.632 153.241 25.2815 5 10584459.mol ... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

7. Shell Programming and Scripting

script to merge two files on an index

I have a need to merge two files on the value of an index column. input file 1 id filePath MDL_NUMBER 1 MFCD00008104.mol MFCD00008104 2 MFCD00012849.mol MFCD00012849 3 MFCD00037597.mol MFCD00037597 4 MFCD00064558.mol MFCD00064558 5 MFCD00064559.mol MFCD00064559 input file 2 ... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

8. Shell Programming and Scripting

Merge multiple lines to one line when line starts with and ends with

example: comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ; in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can... (4 Replies)
Discussion started by: wambli
4 Replies

9. Shell Programming and Scripting

Merge multiple tab delimited files with index checking

Hello, I have 40 data files where the first three columns are the same (in theory) and the 4th column is different. Here is an example of three files, file 2: A_f0_r179_pred.txt Id Group Name E0 1 V N(,)'1 0.2904 2 V N(,)'2 0.3180 3 V N(,)'3 0.3277 4 V N(,)'4 0.3675 5 V N(,)'5 0.3456 ... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

10. Shell Programming and Scripting

Copy of array by index value fails

Hello, I have a complicated situational find and replace that I wrote in bash because I didn't know how to do everything in awk. The code works but is very slow, as expected. To create my modified file, I am looping through an array that was populated earlier and making some replacements at... (6 Replies)
Discussion started by: LMHmedchem
6 Replies
qsort(3C)						   Standard C Library Functions 						 qsort(3C)

NAME
qsort - quick sort SYNOPSIS
#include <stdlib.h> void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)); DESCRIPTION
The qsort() function is an implementation of the quick-sort algorithm. It sorts a table of data in place. The contents of the table are sorted in ascending order according to the user-supplied comparison function. The base argument points to the element at the base of the table. The nel argument is the number of elements in the table. The width argument specifies the size of each element in bytes. The compar argument is the name of the comparison function, which is called with two arguments that point to the elements being compared. The function must return an integer less than, equal to, or greater than zero to indicate if the first argument is to be considered less than, equal to, or greater than the second argument. The contents of the table are sorted in ascending order according to the user supplied comparison function. USAGE
The qsort() function safely allows concurrent access by multiple threads to disjoint data, such as overlapping subtrees or tables. EXAMPLES
Example 1: Program sorts. The following program sorts a simple array: #include <stdlib.h> #include <stdio.h> static int intcompare(const void *p1, const void *p2) { int i = *((int *)p1); int j = *((int *)p2); if (i > j) return(1); if (i < j) return (-1); return(0); } int main() { int i; int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; size_t nelems = sizeof (a) / sizeof (int); qsort((void *)a, nelems, sizeof (int), intcompare); for (i = 0; i < nelems; i++) { (void) printf("%d ", a[i]); } (void) printf(" "); return(0); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sort(1), bsearch(3C), lsearch(3C), string(3C), attributes(5), standards(5) NOTES
The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. The relative order in the output of two items that compare as equal is unpredictable. SunOS 5.10 6 Dec 2004 qsort(3C)
All times are GMT -4. The time now is 06:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy