0
点赞
收藏
分享

微信扫一扫

2022.3.25 数组和冒泡

香小蕉 2022-03-25 阅读 80
c语言

//           2022.3.25    数组   和冒泡   
//    goto  【了解】
//     1、设定一个标签
//     2、使用“goto 标签名”跳转到标签的位置,(只在函数内部生效)
// -------------------------------------------------------
//   数组:
//      相同数据类型的有序连续存储,
//    int arr [ 10 ]   ={1,2,23,4,5,6,10,7,8,9};
//    各个元素的内存地址,连续。
//   数组名为地址,是数组首元素的地址, arr == &arr[0];
//   printf("数组大小:%u\n", sizeof(arr[0]);
//   printf("数组元素的大小:%u\n", sizeof(arr[0]);
//   printf("数组元素个数:%d\n", sizeof(arr) / sizeof(arr[0]);
//  数组初始化:
//   int arr[12] = {1, 2, 4, 6, 76, 8, 90, 4, 3, 6, 6, 8 };
//   int arr[10] = {1, 2, 4, 6, 76, 8, 9 }; 剩余未初始化的元素,默认为0值,
//   int arr[10] = [ 0 ]; 初始化一个全为 0 的数组,【重点】
//   int arr[] = {1, 2, 4, 6,  8 };    编译器自动求取元素个数 【重点】
//   int arr[] = { 0 }; 只有一个元素,值为 0、
//   int arr[10];
//   int [0] = 5;
//   int [1] = 6;
//   int [2] = 7;  其余元素未被初始化,默认值,随机数,
//练习 : 数组元素逆序:
//int arr[] = { 1, 6, 8, 0, 4, 3, 9, 2 }: //{2, 9, 3, 4, 0, 8, 6, 1}
//int len = sizeof(arr) / sizoef(arr[0]); // 数组元素个数
//
//int i = 0;          //  i 表示数组的首元素下标
//int j = len - 1;     //  表示数组的最后一个元素下标
//int xijinping = 0;  // 临时变量
//
//  //  交换 数组元素,做逆序
//while (i < j)
//    {
//    xijinping = arr[i];    //三杯水法变量交换
//    arr[i] = arr[j];
//    arr[j] = xijinping;
//    i++;
//    j == ;
//}
//
//冒泡排序:
//     int xijinping[10] = {12, 32, 14, 62, 27, 8, 89}; ==> 8  12  14  27  32  62  89

//#include <stdio.h>
//
//int main()
//{
//    int a[10]; // 定义了一个数组,名字叫a,有10个成员,每个成员都是int 类型
//     //    a[0].......a[9], 没有a[10]
//    //    没有a 这个变量,a 是数组的名字,但不是变量名,他是常量
//    a[0] = 0;
//    //..........
//    a[9] = 9;
//
//    int i = 0;
//    for (i = 0; i < 10; i++)
//    {
//        a[i] = i; //给数组赋值
//    }
//    //  遍历数组,并输出每个成员的值,
//    for (i = 0; i < 10; i++)
//    {
//        printf("%d", a[i]);
//    }
//    printf("\n");
//    return 0;
//}


//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//#include <windows.h>
//
//int main(void)
//{
//    
//        printf("=================1==============\n");
//        printf("=================2==============\n");
//        LABLE:
//        printf("=================3==============\n");
//        printf("=================4==============\n");
//        printf("=================5==============\n");
//        printf("=================6==============\n");
//        printf("=================7==============\n");
//
//        goto LABLE;
//        printf("=================8==============\n");
//        printf("=================9==============\n");
//        printf("=================10==============\n");
//     
//    system("panse");
//    return EXIT_SUCCESS;
//}

//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//#include <windows.h>
//
//int main(void)
//{
//    int i = 0, j = 0;
//    for (i = 0; i < 10; i++)
//    {
//        if (i == 5)
//            goto ABX234;
//        printf("i = %d\n", i);
//    }
//    for (j = 0; j < 20; j++)
//    {
//    ABX234:
//        printf("j = %d\n", j);
//    }
//
//    system("panse");
//    return 0;
//}

//        ------------------------------------------ 下面的饿程序抄也没超对
//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//
//
//int main(void)
//{
//    int a = 5, b = 29, c = 10;
//    int arr[10] = { 1,2,4,6,76,8,90,4,3,6 };   // int a = 109;
//
//    printf("&arr[0] = %x\n" &arr[0]);
//    printf("&arr[1] = %p\n" &arr[1]);
//    printf("&arr[2] = %p\n" &arr[2]);
//    printf("&arr[3] = %p\n" &arr[3]);
//    printf("&arr[4] = %x\n" &arr[4]);
//
//    printf("&a = %p\n", &a);
//    printf("&b = %p\n", &b);
//    printf("&c = %p\n", &c);
//       
//    system("panse");
//    return EXIT_SUCCESS;
//}
//
//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//
//
//int main(void)
//{
//    int a = 5, b = 29, c = 10;
//    int arr[10] = { 1,2,4,6,76,8,90,4,3,6 };   // int a = 109;
//
//    printf("&arr[0] = %p\n", &arr[0]);  //  取数组首元素的地址
//
//    printf("arr = %p\n", arr);  // 数组名
//    system("panse");
//    return EXIT_SUCCESS;
//}

//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//
//
//int main(void)
//{
//    int a = 5, b = 29, c = 10;
//    int arr[10] = { 1,2,4,6,76,8,90,4,3,6 };   // int a = 109;
//
//    printf("数组大小:%\n", sizeof(arr));
//
//    printf("数组:%\n", sizeof(arr));
//    system("panse");
//    return EXIT_SUCCESS;
//}

//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//
//
//int main(void)
//{
//    
//    int arr[10] = { 1,2,4,6,76,8,9 };   // int a = 109;
//
//    int n = sizeof(arr) / sizeof(arr[0]);
//
//    for (size_t i = 0; i < n; i++)
//    {
//        printf("%d\n", arr[i]);
//    }
//    system("panse");
//    return EXIT_SUCCESS;
//}

//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//
//
//int main(void)
//{
//
//    int arr[10] = {0};   // int a = 109;
//
//    int n = sizeof(arr) / sizeof(arr[0]);
//
//    for (size_t i = 0; i < n; i++)
//    {
//        printf("%d\n", arr[i]);
//    }
//    system("panse");
//    return EXIT_SUCCESS;
//}

//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>
//
//
//int main(void)
//{
//    int arr[] = { 1, 6, 8, 0, 4, 3, 9, 2 }; // {2, 9, 3, 4, 0, 9 ,6, 1}
//    int len = sizeof(arr) / sizeof(arr[0]);//数组元素个数
//
//    int i = 0;  //数组的首元素下标
//    int j = len - 1;  // 表示数组的最后一个元素下标
//    int temp = 0; //  临时变量
//
//     //  打印原始数组
//    for (size_t m = 0; m < len; m++)
//    {
//        printf("%d", arr[m]);
//    }
//    printf("\n");
//    //  交互 数组元素,做逆序
//
//    while (i < j)
//    {
//        temp = arr[i];    //三杯水变量交换
//        arr[i] = arr[j];
//        arr[j] = temp;
//        i++;
//        j--;
//    }
//    for (size_t n = 0; n < len; n++)
//    {
//        printf("%d", arr[n]);
//    }
//    printf("\n");
//   
//    system("panse");
//    return EXIT_SUCCESS;
//}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    int xjp[] = { 12,  32,  14,  62,  27,  8,  89 };

    int n = sizeof(xjp) / sizeof(xjp[0]);  // 数组元素个数

    int temp = 0;  //  临时变量

    //  完成乱序数组的冒泡排序。
    for (size_t i = 0; i < n - 1; i++)  //外层控制行
    {
        for (size_t j = 0; j < n - 1 - i; j++)  //内层控制
        {
            if (xjp[j] > xjp[j + 1])  // 满足条件 三杯水交换
            {
                temp = xjp[j];
                xjp[j] = xjp[j + 1];
                xjp[j + 1] = temp;
            }
        }

    }
    //  打印排序后的数组,确定正确性。
    for (size_t i = 0; i < n; i++)
    {
        printf("%d ", xjp[i]);
    }
    printf("\n");
    system("panse");
    return EXIT_SUCCESS;
}
 

举报

相关推荐

0 条评论